rokdd
rokdd

Reputation: 652

Merge xml in PHP DOM

i want to merge a PHP DOMDocument in another..

//this creates the DOM which will be imported
function element_index(..)
{
$skrit=new DOMDocument();
$skrit->loadXML('<krits:kriti xmlns:krits="http://test.de/krits">..</krits:kriti>');
return $skrit;
}

function crawl_xml($element)
{
//thats the line where to get the result and merge      
$skrit=element_index(..);
$temp3=$skrit->documentElement->cloneNode(true);
$element->appendChild($xpIn->importNode($temp3));
}

//thats how i start the recurisve walking through Nodes
$xpIn = new DOMDocument();
crawl_xml($xpIn->firstChild);

The input/output should like this:

<!--input-->
<all><one/></all>

<!--input new to add-->
<krits:kriti xmlns:krits="http://test.de/krits">..</krits:kriti>

<!--ouput-->
<all><krits:kriti xmlns:krits="http://test.de/krits">..</krits:kriti><one><krits:kriti xmlns:krits="http://test.de/krits">..</krits:kriti></one></all>

maybe importNode might be not a good choice? The cloneNode I used because I hoped to avoid problems with recursive call.. So what i am doing wrong? Thanks for your help!

Upvotes: 1

Views: 2969

Answers (1)

netcoder
netcoder

Reputation: 67735

If you want to import the whole node sub-tree (and not just the node itself), you need to set $deep to true in importNode:

$domDocument->importNode($node, true);

Upvotes: 1

Related Questions