如何轻松地将具有相同父节点的两个XML文档合并到一个文档中? [英] How can I easily combine two XML documents with the same parent node into one document?

查看:126
本文介绍了如何轻松地将具有相同父节点的两个XML文档合并到一个文档中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经确定无法通过SimpleXMLElements做到这一点。我一直在阅读PHP DOMDocument手册,我认为我可以通过迭代来做到这一点,但这似乎效率很低。

I've decided there's no way to do this with SimpleXMLElements. I have been reading the PHP DOMDocument manual, and I think I could do it with iteration, but that seems inefficient. Is there a better way that's not occurring to me?

伪代码式迭代解决方案:

Psuedocode-ish iterative solution:

// two DOMDocuments with same root element
$parent = new ...
$otherParent = new ...

$children = $parent->getElementByTagName('child');
foreach ($children as $child) {
   $otherParent->appendChild($child);
}

为清楚起见,我有两个看起来像这样的XML文档:

For clarity, I have two XML documents that both look like this:

    <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>


推荐答案

作为唯一的公共节点,您可以在两者之间进行识别如果我精确而严格地提出您的问题,则文件将是根节点,因此解决方案将是:

As the only common node you could identify between the two files would be the root node if I take your question precisely and strict so the solution would be:

<doc1:parent>
    <doc1:children>...</>
    <doc2:children>...</>
</doc1:parent>

您写的顺序并不重要,因此您可以在这里看到doc2在doc1之后。两个SimpleXML元素 $ xml1 $ xml2 的示例代码,它们同时包含上述示例XML形式:

You wrote the order is not important, so as you can see here, doc2 comes after doc1. Example code for two SimpleXML elements $xml1 and $xml2 that contain both the example XML form above:

$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表示的文档:

Now $doc1 contains the document represented by this XML:

<?xml version="1.0"?>
<parent>
      <child>
        <childOfChild>
           {etc, more levels of nested XML trees possible}
        </childOfChild>
      </child>
      <child>
        <childOfChild>
           {etc, more levels possible}
        </childOfChild>
      </child>

      <child>
        <childOfChild>
           {etc, more levels of nested XML trees possible}
        </childOfChild>
      </child>
      <child>
        <childOfChild>
           {etc, more levels possible}
        </childOfChild>
      </child>
</parent>

如您所见,两个文档的树都被保留,只有您描述为 same 是根节点(实际上也是两个节点),因此它不会从第二个文档中接管,而仅是其子节点。

As you can see, the trees of both documents are preserved, only the node you describe as same is the root node (actually that are two nodes, too), so it's not taken over from the second document, but only it's children.

这篇关于如何轻松地将具有相同父节点的两个XML文档合并到一个文档中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆