PHP | DOMDocument importNode() Function

The DOMDocument::importNode() function is an inbuilt function in PHP which is used to return a copy of the node which need to import and associates it with the current document.


DOMNode DOMDocument::importNode( DOMNode $importedNode, bool $deep = FALSE )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $importedNode: This parameter holds the node which need to import.
  • $deep: This parameter holds the Boolean value. If it set to TRUE then it will recursively import the subtree under the importedNode.

Return Value: This function returns the copied node on success or FALSE, if it cannot be copied.

Below program illustrates the DOMDocument::importNode() function in PHP:






// Create a new document
$dom = new DOMDocument;
// Load the XML document
// Use getElementsByTagName() function to search
// all elements with given local tag name
$node = $dom->getElementsByTagName("contact")->item(0);
// Create a new document
$dom1 = new DOMDocument;
$dom1->formatOutput = true;
// Load the XML document
echo "Document before copying the nodes\n";
// Save the file in XML and display it
echo $dom1->saveXML();
// Use importNode() function to import the node
$node = $dom1->importNode($node, true);
// Append child to the document
echo "\nDocument after copying the nodes\n";
// Save XML document and display it
echo $dom1->saveXML();



Document before copying the nodes
<?xml version="1.0"?>
Document after copying the nodes
<?xml version="1.0"?>


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.