Dom 简明教程

XML DOM - Accessing

在本章中,我们来学习如何访问被认为是 XML 文档的信息单元的 XML DOM 节点。XML DOM 的节点结构允许开发人员在树中导航以查找特定信息,并同时访问该信息。

In this chapter, we will study about how to access the XML DOM nodes which are considered as the informational units of the XML document. The node structure of the XML DOM allows the developer to navigate around the tree looking for specific information and simultaneously access the information.

Accessing Nodes

以下是可以访问节点的三种方式 -

Following are the three ways in which you can access the nodes −

  1. By using the getElementsByTagName () method

  2. By looping through or traversing through nodes tree

  3. By navigating the node tree, using the node relationships

getElementsByTagName ()

此方法允许通过指定节点名称来访问节点的信息。它还允许访问节点列表和节点列表长度的信息。

This method allows accessing the information of a node by specifying the node name. It also allows accessing the information of the Node List and Node List Length.

Syntax

getElementByTagName() 方法具有以下语法 -

The getElementByTagName() method has the following syntax −

node.getElementByTagName("tagname");

其中,

Where,

  1. node − is the document node.

  2. tagname − holds the name of the node whose value you want to get.

Example

下面是一个简单的程序,说明了 getElementByTagName 方法的使用。

Following is a simple program which illustrates the usage of method getElementByTagName.

<!DOCTYPE html>
<html>
   <body>
      <div>
         <b>FirstName:</b> <span id = "FirstName"></span><br>
         <b>LastName:</b> <span id = "LastName"></span><br>
         <b>Category:</b> <span id = "Employee"></span><br>
      </div>
      <script>
         if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
         } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET","/dom/node.xml",false);
         xmlhttp.send();
         xmlDoc = xmlhttp.responseXML;

         document.getElementById("FirstName").innerHTML =
         xmlDoc.getElementsByTagName("FirstName")[0].childNodes[0].nodeValue;
         document.getElementById("LastName").innerHTML =
         xmlDoc.getElementsByTagName("LastName")[0].childNodes[0].nodeValue;
         document.getElementById("Employee").innerHTML =
         xmlDoc.getElementsByTagName("Employee")[0].attributes[0].nodeValue;
      </script>
   </body>
</html>
  1. In the above example, we are accessing the information of the nodes FirstName, LastName and Employee.

  2. xmlDoc.getElementsByTagName("FirstName")[0].childNodes[0].nodeValue; This line accesses the value for the child node FirstName using the getElementByTagName() method.

  3. xmlDoc.getElementsByTagName("Employee")[0].attributes[0].nodeValue; This line accesses the attribute value of the node Employee getElementByTagName() method.

Traversing Through Nodes

这在带示例的第 DOM Traversing 章中进行了说明。

This is covered in the chapter DOM Traversing with examples.

Navigating Through Nodes

这在带示例的第 DOM Navigation 章中进行了说明。

This is covered in the chapter DOM Navigation with examples.