我已经决定没有办法用 SimpleXMLElements 做到这一点。我一直在阅读 PHP DOMDocument 手册,我想我可以通过迭代来做到这一点,但这似乎效率低下。有没有更好的方法我没有想到?
伪代码迭代解决方案:
// two DOMDocuments with same root element
$parent = new ...
$otherParent = new ...
$children = $parent->getElementByTagName('child');
foreach ($children as $child) {
$otherParent->appendChild($child);
}
为清楚起见,我有两个 XML 文档,它们都如下所示:
<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>