Dom 简明教程

XML DOM - Set Node

在本章中,我们将研究如何更改 XML DOM 对象中节点值。节点值可按如下方式更改 −

In this chapter, we will study about how to change the values of nodes in an XML DOM object. Node value can be changed as follows −

var value = node.nodeValue;

如果节点是 Attribute,则 value 变量将为该属性的值;如果节点是 Text 节点,则将为文本内容;如果节点是 Element,则将为 null。

If node is an Attribute then the value variable will be the value of the attribute; if node is a Text node it will be the text content; if node is an Element it will be null.

以下各节将演示每种节点类型(属性、文本节点和元素)的节点值设置。

Following sections will demonstrate the node value setting for each node type (attribute, text node and element).

所有以下示例中使用的 node.xml 如下:

The node.xml used in all the following examples is as below −

<Company>
   <Employee category = "Technical">
      <FirstName>Tanmay</FirstName>
      <LastName>Patil</LastName>
      <ContactNo>1234567890</ContactNo>
      <Email>tanmaypatil@xyz.com</Email>
   </Employee>

   <Employee category = "Non-Technical">
      <FirstName>Taniya</FirstName>
      <LastName>Mishra</LastName>
      <ContactNo>1234667898</ContactNo>
      <Email>taniyamishra@xyz.com</Email>
   </Employee>

   <Employee category = "Management">
      <FirstName>Tanisha</FirstName>
      <LastName>Sharma</LastName>
      <ContactNo>1234562350</ContactNo>
      <Email>tanishasharma@xyz.com</Email>
   </Employee>
</Company>

Change Value of Text Node

当我们说更改节点元素的值时,我们的意思是编辑元素(也称为文本节点)的文本内容。以下示例演示如何更改元素的文本节点。

When we, say the change value of Node element we mean to edit the text content of an element (which is also called the text node). Following example demonstrates how to change the text node of an element.

Example

以下示例 (set_text_node_example.htm) 将 XML 文档 ( node.xml ) 解析为 XML DOM 对象,并更改元素文本节点的值。此示例中,将每个 Employee 的 Email 更改为 support@xyz.com 并打印其值。

The following example (set_text_node_example.htm) parses an XML document (node.xml) into an XML DOM object and change the value of an element’s text node. In this case, Email of each Employee to support@xyz.com and print the values.

<!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("Email");
         for(i = 0;i<x.length;i++) {

            x[i].childNodes[0].nodeValue = "support@xyz.com";
            document.write(i+');
            document.write(x[i].childNodes[0].nodeValue);
            document.write('<br>');
         }

      </script>
   </body>
</html>

Execution

将此文件保存在服务器路径中,文件名设为 set_text_node_example.htm(此文件和 node.xml 应位于服务器中的同一个路径)。您将收到以下输出 −

Save this file as set_text_node_example.htm on the server path (this file and node.xml should be on the same path in your server). You will receive the following output −

0) support@xyz.com
1) support@xyz.com
2) support@xyz.com

Change Value of Attribute Node

以下示例演示如何更改元素的属性节点。

The following example demonstrates how to change the attribute node of an element.

Example

以下示例(set_attribute_example.htm)将 XML 文档( node.xml )解析成 XML DOM 对象,并更改元素属性节点的值。在本例中,将每个 Employee 的 Category 分别更改为 admin-0、admin-1、admin-2,并打印值。

The following example (set_attribute_example.htm) parses an XML document (node.xml) into an XML DOM object and changes the value of an element’s attribute node. In this case, the Category of each Employee to admin-0, admin-1, admin-2 respectively and print the values.

<!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");
         for(i = 0 ;i<x.length;i++){

            newcategory = x[i].getAttributeNode('category');
            newcategory.nodeValue = "admin-"+i;
            document.write(i+');
            document.write(x[i].getAttributeNode('category').nodeValue);
            document.write('<br>');
         }

      </script>
   </body>
</html>

Execution

将此文件另存为 set_node_attribute_example.htm,并放在服务器路径上(此文件和 node.xml 应位于服务器上的同一路径)。结果如下所示 −

Save this file as set_node_attribute_example.htm on the server path (this file and node.xml should be on the same path in your server). The result would be as below −

0) admin-0
1) admin-1
2) admin-2