分组XML对象

时间:2011-08-04 15:49:50

标签: php xml simplexml

寻找一种方法将几个Simple XML对象一起添加。目标是将它们作为单个XML文档输出,但作为XML dom中的单独条目。我不知道如何做到这一点。对象是这样的数组:

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
                )

1 个答案:

答案 0 :(得分:1)

为此我会回复DOM,只有那么多 Simple 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();