DOMElement cloning and appending: 'Wrong Document Error'

后端 未结 2 1894
我在风中等你
我在风中等你 2020-12-17 07:47

There\'s something I don\'t fully understand about node cloning with the PHP\'s DOM api. Here\'s a sample file that quickly duplicates the issue I\'m coming across.

相关标签:
2条回答
  • 2020-12-17 08:35

    Use DOMDocument->importNode to import the node into the other document before adding it to the DOM.

    0 讨论(0)
  • 2020-12-17 08:40

    You'll have to append the result of the importNode method to the DOM. Keep in mind that the method could return false when it cannot be imported

    if ($importedNode = $doc2->importNode($root->cloneNode())) {
        $root2->appendChild($importedNode);
    }
    

    If you need to import the node, all of it's child nodes (resursively) and/or the node's attributes use the optional second parameter deep:

    if ($importedNode = $doc2->importNode($root->cloneNode(), true)) {
        $root2->appendChild($importedNode);
    }
    
    0 讨论(0)
提交回复
热议问题