PHP: DOMDocument::adoptNode - Manual
(PHP >= 8.3)
DOMDocument::adoptNode — Transfer a node from another document
Description
Parameters
node-
The node to transfer.
Return Values
The node that was transferred, or false on error.
Examples
Example #1 DOMDocument::adoptNode() example
Transfers the hello element from the first document to the second one.
<?php
$doc1 = new DOMDocument;
$doc1->loadXML("<container><hello><world/></hello></container>");
$hello = $doc1->documentElement->firstChild;
$doc2 = new DOMDocument;
$doc2->loadXML("<root/>");
$doc2->documentElement->appendChild($doc2->adoptNode($hello));
echo $doc1->saveXML() . PHP_EOL . PHP_EOL;
echo $doc2->saveXML();
?>The above example will output:
<?xml version="1.0"?> <container/> <?xml version="1.0"?> <root><hello><world/></hello></root>