2011-08-04 72 views
0

尋找一種將幾個Simple XML對象添加在一起的方法。目標是將它們作爲單個XML文檔輸出,但作爲XML DOM中的單獨條目輸出。我不知道如何完成這一點。對象是在這樣一個數組:將XML對象分組

Array ( 
[0] => Array 
(
    [0] => SimpleXMLElement Object 
     (
      [@attributes] => Array 
       (
        [no] => 23432423 
        [type] => Array 
       ) 

      [id] => 40043030 
      [title] => Cars 
      [cinemadate] => 2011-07-06 
      [changedate] => 2011-07-27T10:19:00 
      [year] => 2011 
      [length] => 112 
      [genres] => SimpleXMLElement Object 
       (
        [genre] => animatie 
       ) 


[1] => Array 
(
    [0] => SimpleXMLElement Object 
     (
      [@attributes] => Array 
       (
        [no] => 48050593 
        [type] => Array 
       ) 

      [id] => 1231676 
      [title] => Arrietty 
      [cinemadate] => 2011-07-06 
      [changedate] => 2011-06-21T10:39:00 
      [genres] => SimpleXMLElement Object 
       (
        [genre] => animatie 
       ) 
+0

什麼是你的XML看起來相似嗎? – ajreal

回答

1

我將回到DOM這一點,就只有這麼多簡單 XML可以這樣做:

$node = new SimpleXMLElement('<root/>'); 
$domnode = dom_import_simplexml($node); 
foreach($arr as $simplexmlelement){ 
    $domnode->appendChild(
     $domnode->ownerDocument->importNode(dom_import_simplexml($simplexmlelement),true) 
    ); 
} 
$node = simplexml_import_dom($node); 
echo $node->asXML();