Syntax:DOMNode DOMDocument::importNode (DOMNode $importedNode, bool $deep = FALSE)
Parameters:This function takes two parameters as above and described below: - $importNode :this parameter contains the node to be imported.
- $deep:this parameter contains a boolean value. If set to TRUE, it will recursively import the subtree in the imported node.
Return Value:This function returns the copied node on success or FALSE if it is not possible copy.The following program illustrates the DOMDocument::importNode() function in PHP:Program :
// Create a new one document
$dom
=
new
DOMDocument;
// Load XML document
$dom
-> loadXML ("< root > < contact > < email > [email protected]< / email >
< mobile > + 91-987654321 < / mobile > < / contact > < / root > ");
// Use the getElementsByTagName() function to search
// all elements with the given local tag name
$node
=
$dom
-> getElementsByTagName (
" contact "
) -> item (0);
// Create a new document
$dom1
=
new
DOMDocument;
$dom1
-> formatOutput = true;
// Load XML document
$dom1
-> loadXML ("< root > < contactinfo > < email > [email protected]< / email >
< mobile > + 91-987654321 < / mobile > < / contactinfo > < / root > ");
echo
" Document before copying the nodes "
;
// Save the file as XML and display it
echo
$dom1
-> saveXML();
// Use the importNode() function to import the node
$node
=
$dom1
-> importNode (
$node
, true);
// Add a child element to the document
$dom1
-> documentElement-> appendChild (
$node
);
echo
" Document after copying the nodes "
;
// Save the XML document and display it
echo
$dom1
-> saveXML();
?>
Exit:Document before copying the nodes < root > < contactinfo > < email > [email protected]< / email > < mobile > + 91-987654321 < / mobile > < / contactinfo > < / root > Document after copying the nodes < contactinfo > < email > [email protected]< / email > < mobile > + 91-987654321 < / mobile > < / contactinfo > < contact > < email > [email protected]< / email > < mobile > + 91-987654321 < / mobile > < / contact > < / root >
Link: https://www.php.net/ manual / en / domdocument.importnode.php