XML DOM Knoten Beroepen
- Previous page DOM properties and methods
- Next page DOM node information
通过 DOM,您能够访问 XML 文档中的每个节点。
"tagname"
下面的例子使用 XML 文件 books.xml.
函数 loadXMLDoc(),位于外部 JavaScript 中,用于加载 XML 文件。
- 使用节点列表中的下标号来访问节点
- 本例使用 getElementsByTagname() 方法来获得 "books.xml" 中的第三个 <title> 元素。
- 使用 length 属性来循环节点
- 本例使用 length 属性来循环 "books.xml" 中的所有 <title> 元素。
- 查看元素的节点类型
- 本例使用 nodeType 属性来获得 "books.xml" 中根元素的节点类型。
- 循环元素节点
- 本例使用 nodeType 属性来处理 "books.xml" 中的元素节点。
- 使用节点的关系来循环元素节点
- 本例使用 nodeType 属性和 nextSibling 属性来处理 "books.xml" 中的元素节点。
访问节点
您可以通过三种方法来访问节点:
- 通过使用 getElementsByTagName() 方法
- 通过循环(遍历)节点树
- 通过利用节点的关系在节点树中导航
getElementsByTagName() 方法
getElementsByTagName() 返回拥有指定标签名的所有元素。
语法
node.getElementsByTagName("tagname");
"tagname"
);
Example
x.getElementsByTagName("title");
Please note that the example above only returns the <title> elements under the x node. To return all <title> elements in the XML document, use:
xmlDoc.getElementsByTagName("title");
Here, xmlDoc is the document itself (document node).
DOM Node List
getElementsByTagName() method returns a node list (node list). A node list is an array of nodes. loadXMLDoc() Set "books.xmlThe following code uses
xmlDoc=loadXMLDoc("books.xml"); You can loop through a node list by using the length property:
"Load xmlDoc into, then store a list of <title> nodes in the variable x:
You can access the <title> element in x by index. To access the third <title>, you can write:
y=x[2];Note:
The index starts at 0.
In a later section of this tutorial, you will learn more about Node Lists.
DOM Node List Length
The length property defines the length of the node list (i.e., the number of nodes).
xmlDoc=loadXMLDoc("books.xml"); You can loop through a node list by using the length property: for (i=0;i<x.length;i++) { x=xmlDoc.getElementsByTagName("title"); document.write("<br />"); }
Example explanation:
- document.write(x[i].childNodes[0].nodeValue); loadXMLDoc() Set "books.xmlUse
- "Load xmlDoc
- Get all <title> element nodes
Output the value of each text node of the <title> element
Node Type XML document's documentElement
Attributes of the node are: Attributes are the root node. nodeName
Attributes of the node are: nodeType Attributes are the type of the node.
You will learn more about node attributes in the next section of this tutorial.
Traverse the node
The following code loops through the child nodes of the root node, which are also element nodes:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement.childNodes; for (i=0;i<x.length;i++) { if (x[i].nodeType==1) {//Process only element nodes (type 1) document.write(x[i].nodeName); document.write("<br />"); } }
Example explanation:
- By using loadXMLDoc() Set "books.xml"Load xmlDoc in"
- Get the child nodes of the root element
- Check the node type of each child node. If the node type is "1", it is an element node
- If it is an element node, output the name of the node
Navigate using node relationships
The following code navigates through the node tree by using the relationship between nodes:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0].childNodes; y=xmlDoc.getElementsByTagName("book")[0].firstChild; for (i=0;i<x.length;i++) { if (y.nodeType==1) {//Process only element nodes (type 1) document.write(y.nodeName + "<br />"); } y=y.nextSibling; }
- By using loadXMLDoc() Set "books.xml"Load xmlDoc in"
- Get the first child of the first "book" element
- Set the "y" variable to the first child of the first "book" element
- Check the node type of each child node, if the node type is "1", it is an element node
- If it is an element node, output the name of the node
- Set the "y" variable to the next sibling node and run the loop again
- Previous page DOM properties and methods
- Next page DOM node information