Dom 简明教程

XML DOM - Clone Node

在本章中,我们将讨论 XML DOM 对象上的克隆节点操作。克隆节点操作用于创建指定节点的副本。使用 cloneNode() 进行此操作。

In this chapter, we will discucss the Clone Node operation on XML DOM object. Clone node operation is used to create a duplicate copy of the specified node. cloneNode() is used for this operation.

cloneNode()

此方法返回此节点的副本,即用作节点的通用副本构造函数。副本节点没有父节点(parentNode 为 null)也没有用户数据。

This method returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes. The duplicate node has no parent ( parentNode is null) and no user data.

Syntax

cloneNode() 方法具有以下语法 −

The cloneNode() method has the following syntax −

Node cloneNode(boolean deep)
  1. deep − If true, recursively clones the subtree under the specified node; if false, clone only the node itself (and its attributes, if it is an Element).

  2. This method returns the duplicate node.

Example

以下示例(clonenode_example.htm)将 XML 文档 ( node.xml ) 解析成一个 XML DOM 对象,并创建第一个 Employee 元素的深度副本。

The following example (clonenode_example.htm) parses an XML document (node.xml) into an XML DOM object and creates a deep copy of the first Employee element.

<!DOCTYPE html>
<html>
   <head>
      <script>
         function loadXMLDoc(filename) {
            if (window.XMLHttpRequest) {
               xhttp = new XMLHttpRequest();
            } else // code for IE5 and IE6 {
               xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xhttp.open("GET",filename,false);
            xhttp.send();
            return xhttp.responseXML;
         }
      </script>
   </head>
   <body>
      <script>
         xmlDoc = loadXMLDoc("/dom/node.xml");

         x = xmlDoc.getElementsByTagName('Employee')[0];
         clone_node = x.cloneNode(true);
         xmlDoc.documentElement.appendChild(clone_node);

         firstname = xmlDoc.getElementsByTagName("FirstName");
         lastname = xmlDoc.getElementsByTagName("LastName");
	 contact = xmlDoc.getElementsByTagName("ContactNo");
	 email = xmlDoc.getElementsByTagName("Email");
         for (i = 0;i < firstname.length;i++) {
            document.write(firstname[i].childNodes[0].nodeValue+'
               '+lastname[i].childNodes[0].nodeValue+',
               '+contact[i].childNodes[0].nodeValue+',  '+email[i].childNodes[0].nodeValue);
            document.write("<br>");
         }
      </script>
   </body>
</html>

如您在上述示例中所见,我们已将 cloneNode() 参数设置为 true。因此,Employee 元素下的每个子元素都会被复制或克隆。

As you can see in the above example, we have set the cloneNode() param to true. Hence each of the child element under the Employee element is copied or cloned.

Execution

将此文件保存在服务器路径上并命名为 clonenode_example.htm(该文件和 node.xml 应在您服务器中的同一路径下)。我们将会得到如下所示的输出 −

Save this file as clonenode_example.htm on the server path (this file and node.xml should be on the same path in your server). We will get the output as shown below −

Tanmay Patil, 1234567890, tanmaypatil@xyz.com
Taniya Mishra, 1234667898, taniyamishra@xyz.com
Tanisha Sharma, 1234562350, tanishasharma@xyz.com
Tanmay Patil, 1234567890, tanmaypatil@xyz.com

您会注意到,第一个 Employee 元素被完整克隆了。

You will notice that the first Employee element is cloned completely.