$dom1 = new DomDocument(); $dom1->loadXML(''); $dom2 = new DomDocument(); $newNode = $dom2->importNode($dom1->documentElement->firstChild, true); $dom2->appendChild($newNode); echo $dom2->saveXML(); // Output: Example Example
$dom = new DomDocument(); $dom->loadXML('In this example, we have a single DomDocument with a node called "node". We clone this node and append it to the document element. Finally, we output the XML of the document, which contains two "node" nodes. The package library for this function is the built-in PHP DomDocument class.'); $newNode = $dom->importNode($dom->documentElement->firstChild, true); $dom->documentElement->appendChild($newNode); echo $dom->saveXML(); // Output: Example Example Example