PHP | DOMNode cloneNode() function
Last Updated :
25 Feb, 2020
The DOMNode::cloneNode() function is an inbuilt function in PHP which is used to create a copy of the node.
Syntax:
DOMNode DOMNode::cloneNode( bool $deep )
Parameters:This function accepts a single parameter $deep which indicates whether to copy all descendant nodes. This parameter is set to FALSE by default.
Return Value: This function returns the cloned node.
Program 1:
<?php
$doc = new DOMDocument();
$doc ->loadXML( '<html></html>' );
$h1 = $doc ->createElement( 'h1' , "geeksforgeeks" );
$doc ->documentElement->appendChild( $h1 );
$doc_new = new DOMDocument();
$doc_new = $doc ->cloneNode(true);
echo $doc_new ->saveXML();
?>
|
Output:
Program 2:
<?php
$doc = new DOMDocument( '1.0' , 'iso-8859-1' );
$doc ->loadXML( '<html></html>' );
$h1 = $doc ->createElement( 'h1' , "geeksforgeeks" );
$doc ->documentElement->appendChild( $h1 );
$doc_new = $doc ->cloneNode(false);
echo $doc_new ->saveXML();
?>
|
Output: Press Ctrl + U to see the DOM
Reference: https://www.php.net/manual/en/domnode.clonenode.php
Share your thoughts in the comments
Please Login to comment...