XML DOM Cloning Nodes
- Append this new node to the root node of the XML document Previous Page
- Next Page DOM Examples
Clone Node
cloneNode()
method creates a copy of the specified node.
cloneNode()
The method has one parameter (true
or false
This parameter indicates whether the copied node should contain all properties and child nodes of the original node.
The following code snippet copies the first <book> element and appends it to the root node of the document:
Example
oldNode = xmlDoc.getElementsByTagName('book')[0]; newNode = oldNode.cloneNode(true); xmlDoc.documentElement.appendChild(newNode);
Try It Yourself
- Example Explanation: Assume books.xml
has been loaded to
xmlDoc - in
Get the node to be copied (
oldNode - )
By using
cloneNodeMethod to copy the node to "
newNode - "
- Append this new node to the root node of the XML document Previous Page
- Next Page DOM Examples