在PHP DOM中合并xml [英] Merge xml in PHP DOM
本文介绍了在PHP DOM中合并xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个PHP DOMDocument合并到另一个文件中.
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>
也许importNode可能不是一个好选择?我使用cloneNode的原因是希望避免递归调用的问题.所以我在做什么错呢?感谢您的帮助!
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!
推荐答案
如果要导入整个节点子树(而不仅仅是节点本身),则需要设置 $ deep 到importNode
中的 true :
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);
这篇关于在PHP DOM中合并xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文