clonenode 附录 插入dom xml php之前



我有一个XML,我会克隆父亲,然后在克隆的节点下离开。更多给出此错误。我想知道

如何

致命错误:在C: xampp htdocs xml2 xml2 clonenew.php上,在第32行32

上调用c: xampp htdocs xml2 xml2 xml2 xml2 xml2 xml2 xml2 xml2 xml2
$xmla = <<<XML
<?xml version="1.0" ?>
<library>
  <book isbn="1001" pubdate="1943-01-01">
    <title><![CDATA[The Fountainhead]]></title>
    <author>Ayn Rand</author>
    <price>300</price>
  </book>
  <book isbn="1002" pubdate="1954-01-01">
    <title><![CDATA[The Lord of the Rings]]></title>
    <author>J.R.R.Tolkein</author>
    <price>500</price>
  </book>
  <book isbn="1006" pubdate="1982-01-01">
    <title><![CDATA[The Dark - Tower San]]></title>
    <author>Stephen King</author>
    <price>200</price>
  </book>
</library>
XML;
$xmlb = <<<XML
<?xml version="1.0" ?>
<library>
  <book isbn="1004" pubdate="1943-01-01">
    <title><![CDATA[The Fountainhead]]></title>
    <author>Ayn Rand</author>
    <price>300</price>
  </book>
</library>
XML;
$dom_01 = new DOMDocument();
$dom_01->loadXML($xmla);
$library_01 = $dom_01->documentElement;
$dom_02 = new DOMDocument();
$dom_02->loadXML($xmlb);
$library_02 = $dom_02->documentElement;
$xpath = new DOMXPath($dom_02);
$result = $xpath->query('/library/book[translate(@pubdate,"-","")>translate("1980-01-01","-","")]');
$library_02 = $library_02->cloneNode(true);
$newElement = $library_01->appendChild($result->item(0));
$library_01->parentNode->insertBefore($newElement, $result->item(0));
header("Content-type: text/xml");
echo $dom->saveXML();

Result:
$xmla = <<<XML
<?xml version="1.0" ?>
<library>
  <book isbn="1001" pubdate="1943-01-01">
    <title><![CDATA[The Fountainhead]]></title>
    <author>Ayn Rand</author>
    <price>300</price>
  </book>
  <book isbn="1002" pubdate="1954-01-01">
    <title><![CDATA[The Lord of the Rings]]></title>
    <author>J.R.R.Tolkein</author>
    <price>500</price>
  </book>
  <book isbn="1004" pubdate="1943-01-01">
    <title><![CDATA[The Fountainhead]]></title>
    <author>Ayn Rand</author>
    <price>300</price>
  </book>
  <book isbn="1006" pubdate="1982-01-01">
    <title><![CDATA[The Dark - Tower San]]></title>
    <author>Stephen King</author>
    <price>200</price>
  </book>
</library>
XML;

您正在尝试获取parentNode的CC_1不存在此类节点。

另外,如果要将一个节点从一个文档放入另一个文档中,则使用DOMDocument.importNode代替Clonenode。

最新更新