我有两个 XML 数据源,我想将它们与 PHP 脚本合并到一个 XML 文档中。
第一个源 XML 文档
<book>
<id>1</id>
<title>Republic</title>
</book>
第二个源 XML 文档
<data>
<author>Plato</author>
<language>Greek</language>
</data>
我想将两者结合起来得到
<book>
<id>1</id>
<title>Republic</title>
<author>Plato</author>
<language>Greek</language>
</book>
但我得到的是
<book>
<id>1</id>
<title>Republic</title>
<data>
<author>Plato</author>
<language>Greek</language>
</data></book>
这是我的代码
$first = new DOMDocument("1.0", 'UTF-8');
$first->formatOutput = true;
$first->loadXML(firstXML);
$second = new DOMDocument("1.0", 'UTF-8');
$second->formatOutput = true;
$second->loadXML(secondXML);
$second = $second->documentElement;
$first->documentElement->appendChild($first->importNode($second, TRUE));
$first->saveXML();
$xml = new DOMDocument("1.0", 'UTF-8');
$xml->formatOutput = true;
$xml->appendChild($xml->importNode($first->documentElement,true));
return $xml->saveXML();