脚本宝典收集整理的这篇文章主要介绍了php – 如何轻松地将两个XML文档与同一父节点合并为一个文档?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
// two DOMDocuments wITh same root element $parent = new ... $otherParent = new ... $children = $parent->getElementByTagName('child'); foreach ($children as $child) { $otherParent->apPEndChild($child); }
<parent> <child> <childOfchild> {etc,more levels of nested XML trees possible} </childOfChild> </child> <child> <childOfChild> {etc,more levels possible} </childOfChild> </child> </parent>
我希望输出如下:
<parent> {all children of both original XML docs,order unimportant,that PReserves any nested XML trees the children may have} <parent>
<doc1:parent> <doc1:children>...</> <doc2:children>...</> </doc1:parent>
你写的订单并不重要,所以你可以在这里看到,doc2来自doc1.两个SimpleXML元素$xml1和$xml2的示例代码,它们包含上面的示例XML表单:
$doc1 = dom_import_simplexml($xml1)->ownerDocument; foreach (dom_import_simplexml($xml2)->childNodes as $child) { $child = $doc1->importNode($child,TRUE); echo $doc1->saveXML($child),"\n"; $doc1->documentElement->appendChild($child); }
现在$doc1包含此XML表示的文档:
<?xml version="1.0"?> <parent> <child> <childOfChild> {etc,more levels possible} </childOfChild> </child> <child> <childOfChild> {etc,more levels possible} </childOfChild> </child> </parent>
正如您所看到的,两个文档的树都被保留,只有您描述为相同的节点是根节点(实际上也是两个节点),所以它不会从第二个文档中接管,而只是它的子节点.
以上是脚本宝典为你收集整理的php – 如何轻松地将两个XML文档与同一父节点合并为一个文档?全部内容,希望文章能够帮你解决php – 如何轻松地将两个XML文档与同一父节点合并为一个文档?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。