In the examples below, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().
Copy a node and add it to the node list
This example uses cloneNode() to copy a node and append it to a node list.
The cloneNode() method creates a copy of a specified node.
The cloneNode() method has a parameter (true or false). This parameter indicates if the cloned node should include all attributes and child nodes of the original node.
The following code fragment copies the first <book> node and then adds the copy to the end of the node list:
xmlDoc=loadXMLDoc("books.xml"); var oldNode=xmlDoc.getElementsByTagName(‘book‘)[0]; var newNode=oldNode.cloneNode(true); xmlDoc.documentElement.appendChild(newNode); //Output all titles var y=xmlDoc.getElementsByTagName("title"); for (i=0;i<y.length;i++) { document.write(y[i].childNodes[0].nodeValue); document.write("<br />"); } |
Output:
Everyday Italian Harry Potter XQuery Kick Start Learning XML Everyday Italian |
聯(lián)系客服