XML DOM - 设置节点

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

var value = node.nodeValue;

如果 nodeAttribute,则 value 变量将是属性的值;如果 nodeText 节点,则它将是文本内容;如果 nodeElement,则它将为 null

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

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

<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>

更改文本节点的值

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

示例

以下示例 (set_text_node_example.htm) 将 XML 文档 (node.xml) 解析为 XML DOM 对象并更改元素文本节点的值。在本例中,将每个员工电子邮件发送至support@xyz.com并打印值。

<!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>

执行

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

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

更改属性节点的值

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

示例

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

<!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>

执行

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

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