How to use DOM Clone Node in XML

This article describe about DOM clone node in XML.
  • 2574

XML DOM Clone Nodes

DOM cloneNode creates the clone node. Which is accurate replica of this node, but it has the some exception that it can't have  a parent node.

Copy a Node

For copy of specific node cloneNode() method creates, cloneNode() method has a parameter which has the two value (true or false). This parameter show that if cloned node should contain all attributes and child nodes of it's original node.

xmlDoc=loadXMLDoc("books.xml");
oldNode=xmlDoc.getElementsByTagName('book')[0];
newNode=oldNode.cloneNode(true);
xmlDoc.documentElement.appendChild(newNode);
//Output all titles
y=xmlDoc.getElementsByTagName("title");
for (i=0;i<y.length;i++)
{
document.write(y[i].childNodes[0].nodeValue);
document.write("<br />");
}

 

Note: This part of code specified the first <book> and appends it to the root node of the document.

Output of this program

Everyday indian
jhon disuja
XQuery quick Start
Learning XML
Everyday indian

 Example explained:

  1. First "book.xml" load into the xmlDoc usnig the loadXMLDoc() method.
  2. Then the node copy
  3. By using the cloneNode method copy the node into "newNode"
  4. The root node of  XML append the new node of XML document.
  5. The ouput  all titles in the document

Further Readings

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.