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.
Syntax:
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:
Program:
<?php
$dom = new DOMDocument;
$dom ->loadXML("<root><contact><email>abc@geeksforgeeks.org</email>
<mobile>+91-987654321</mobile></contact></root>");
$node = $dom ->getElementsByTagName( "contact" )->item(0);
$dom1 = new DOMDocument;
$dom1 ->formatOutput = true;
$dom1 ->loadXML("<root><contactinfo><email>abc@geeksforgeeks.org</email>
<mobile>+91-987654321</mobile></contactinfo></root>");
echo "Document before copying the nodes\n" ;
echo $dom1 ->saveXML();
$node = $dom1 ->importNode( $node , true);
$dom1 ->documentElement->appendChild( $node );
echo "\nDocument after copying the nodes\n" ;
echo $dom1 ->saveXML();
?>
|
Output:
Document before copying the nodes
<?xml version="1.0"?>
<root>
<contactinfo><email>abc@geeksforgeeks.org</email>
<mobile>+91-987654321</mobile></contactinfo>
</root>
Document after copying the nodes
<?xml version="1.0"?>
<root>
<contactinfo><email>abc@geeksforgeeks.org</email>
<mobile>+91-987654321</mobile></contactinfo>
<contact><email>abc@geeksforgeeks.org</email>
<mobile>+91-987654321</mobile></contact>
</root>
Reference: https://www.php.net/manual/en/domdocument.importnode.php
Last Updated :
27 Aug, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...