(PHP 5, PHP 7)
Import node into current document
public DOMNode DOMDocument::importNode ( DOMNode $importedNode [, bool $deep ] )
This function returns a copy of the node to import and associates it with the current document.
Parameters:
importedNode
The node to import.
deep
If set to TRUE
, this method will recursively import the subtree under the importedNode
.
Note:
To copy the nodes attributes
deep
needs to be set toTRUE
Returns:
The copied node or FALSE
, if it cannot be copied.
Exception:
DOMException is thrown if node cannot be imported.
Examples:
DOMDocument::importNode() example
Copying nodes between documents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php $orgdoc = new DOMDocument; $orgdoc ->loadXML( "<root><element><child>text in child</child></element></root>" ); // The node we want to import to a new document $node = $orgdoc ->getElementsByTagName( "element" )->item(0); // Create a new document $newdoc = new DOMDocument; $newdoc ->formatOutput = true; // Add some markup $newdoc ->loadXML( "<root><someelement>text in some element</someelement></root>" ); echo "The 'new document' before copying nodes into it:\n" ; echo $newdoc ->saveXML(); // Import the node, and all its children, to the document $node = $newdoc ->importNode( $node , true); // And then append it to the "<root>" node $newdoc ->documentElement->appendChild( $node ); echo "\nThe 'new document' after copying the nodes into it:\n" ; echo $newdoc ->saveXML(); ?> |
The above example will output:
The 'new document' before copying nodes into it: <?xml version="1.0"?> <root> <someelement>text in some element</someelement> </root> The 'new document' after copying the nodes into it: <?xml version="1.0"?> <root> <someelement>text in some element</someelement> <element> <child>text in child</child> </element> </root>
Please login to continue.