Merge two xml documents into one replacing original root elements with new root

Oriefrituen

New Member
I have two data sources in XML which I want to merge in one XML document with a PHP script. First source XML document\[code\]<book> <id>1</id> <title>Republic</title></book>\[/code\]Second source XML document\[code\]<data> <author>Plato</author> <language>Greek</language></data>\[/code\]I want to combine the two to get\[code\]<book> <id>1</id> <title>Republic</title> <author>Plato</author> <language>Greek</language></book>\[/code\]But what I get is\[code\]<book> <id>1</id> <title>Republic</title><data> <author>Plato</author> <language>Greek</language></data></book>\[/code\]This is my code\[code\]$first = new DOMDocument("1.0", 'UTF-8');$first->formatOutput = true;$first->loadXML(firstXML);$second = new DOMDocument("1.0", 'UTF-8');$second->formatOutput = true;$second->loadXML(secondXML);$second = $second->documentElement;$first->documentElement->appendChild($first->importNode($second, TRUE));$first->saveXML();$xml = new DOMDocument("1.0", 'UTF-8');$xml->formatOutput = true;$xml->appendChild($xml->importNode($first->documentElement,true));return $xml->saveXML();\[/code\]
 
Back
Top