2016-12-10 115 views
1

我使用的是this link的Array2XML,它工作的很棒!将根节点后的节点和属性添加到Array2XML

但我需要在输出之前添加一些节点。我需要我的结构是这样的:

<clients> 
    <client>    -->Need to add 
     <id>myid</id>  -->Need to add 
     <name>name</name> -->Need to add 
     <items>   -->Need to add 
     <item> 
      <title>itemtitle</title> 
      <date>itemdate</date> 
     </item> 
     </items> 
    </client> 
<clients> 

但所有我能得到的是:

<clients> 
    <item> 
     <title>itemtitle</title> 
     <date>itemdate</date> 
    </item> 
<clients> 

根节点clients和节点item我能输出,但我怎么可以添加节点client和atributes idname,以及节点item之前的子节点items

这是PHP函数至极我想我需要做出改变,但没有成功:

public static function &createXML($node_name, $arr=array()) { 

    $xml = self::getXMLRoot(); 
    $xml->appendChild(self::convert($node_name, $arr));  

    self::$xml = null; // clear the xml node in the class for 2nd time use. 
    return $xml; 
} 

我已经试过,但它不工作...

public static function &createXML($node_name, $arr=array()) { 

    $xml = self::getXMLRoot(); 
    $clientname='client'; 
    $client = $xml->createElement($clientname); 
    $xml->appendChild(self::convert($node_name, $arr));  

    self::$xml = null; // clear the xml node in the class for 2nd time use. 
    return $xml; 
} 

如何在项目循环之前添加此节点和属性?

非常感谢!

回答

0

好吧,我知道了一些头部划伤后...

我只需要编辑这个:

public static function &createXML($node_name, $arr=array()) { 

    $xml = self::getXMLRoot(); 

    $clients = $xml->createElement("clients"); 
    $xml->appendChild($clients); 

    $client = $xml->createElement("client"); 
    $clients->appendChild($client); 

    $id = $xml->createElement('id', 'myid'); 
    $client->appendChild($id); 
    $name = $xml->createElement('name', 'myname'); 
    $client->appendChild($name); 

    $client->appendChild(self::convert($node_name, $arr));  

    self::$xml = null; // clear the xml node in the class for 2nd time use. 
    return $xml; 
}