分享

XML DOM Clone Nodes

 pengyan 2006-11-30

XML DOM Clone Nodes

prev next

Examples

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.


Copy a Node

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


prev next

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多