2016-12-10 113 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; 
}