当前位置: 首页 > 网络学院 > XML相关教程 > XML DOM > DOM 克隆节点

XML DOM
DOM 节点
DOM 节点列表
DOM 解析
DOM 遍历节点树
DOM Mozilla 和 IE
DOM 获取节点
DOM 设置节点
DOM 删除节点
DOM 更换节点
DOM 建立节点
DOM 添加节点
DOM 克隆节点
DOM 节点类型
DOM Node
DOM NodeList
DOM NamedNodeMap
DOM Document
DOM DocumentType
DOM ProcessingInstr
DOM Element

XML DOM 中的 DOM 克隆节点


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 812 ::
收藏到网摘: n/a

Examples
案例

In the examples below, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().
在下述案例中,我们会使用到下面这两个XML文件:books.xml和Java脚本函数 loadXMLDoc()

 复制一个节点并将它添加到节点列表中
This example uses cloneNode() to copy a node and append it to a node list.
这个案例使用了cloneNode()方法复制了一个节点并将它添加到了一个节点列表中。


Copy a Node
复制一个节点

The cloneNode() method creates a copy of a specified node.
cloneNode()方法的作用是创建一个指定节点的复制版本。

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.
cloneNode()方法拥有一个参数(true或false)。这个参数指明了被克隆的节点是否应该包含所有的属性以及原始节点中的子节点。

The following code fragment copies the first <book> node and then adds the copy to the end of the node list:
下述代码片断复制了第一个<book>节点并将它添加到节点列表的末尾:

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

评论 (0) All

登陆 | 还没注册?