Skip to content
Related Articles

Related Articles

Improve Article

PHP | DOMDocument importNode() Function

  • Last Updated : 27 Aug, 2019
Geek Week

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
Recommended Articles
Page :