How to use DOM Clone Node in XML
Posted in
XML | DTD July 09, 2019
This article describe about DOM clone node in XML.
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:
- First "book.xml" load into the xmlDoc usnig the loadXMLDoc() method.
- Then the node copy
- By using the cloneNode method copy the node into "newNode"
- The root node of XML append the new node of XML document.
- 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