PHP - Importing XML Node



I'm trying to import one node from one document to another. This throws an 'Wrong Document Error' error:



$xml = <<<XML
<root>
<child1>foo </child1>
<child2> bar</child2>
</root>
XML;

$doc = new DOMDocument;
$new = new DOMDocument;

$doc->loadXML($xml);
$xp = new DOMXPath($doc);

foreach($xp->query('/root/node()') as $element)
{
$new->appendChild($doc->importNode($element, true));
}

echo $new->saveXML();

No comments:

Post a Comment