XML DOM childNodes 属性

Document object reference manual

定义和用法

childNodes 属性可返回 document 的子节点的 NodeList。

语法:

documentObject.childNodes

Tips and comments:

Tip:Use the length property of NodeList to determine the number of nodes in the node list. After you know the length of the node list, you can easily iterate through the nodes and extract the values you need!

Example

In all examples, we will use the XML file books.xml, and JavaScript functions loadXMLDoc().

The following code snippet can display the child nodes of the XML document:

xmlDoc=loadXMLDoc("/example/xdom/books.xml");
var x=xmlDoc.childNodes;
for (i=0;i<x.length;i++)
  {
  document.write("Nodename: " + x[i].nodeName)
  document.write(" (nodetype: " + x[i].nodeType + ")<br />")
  }

IE output:

Nodename: xml (nodetype: 7)
Nodename: #comment (nodetype: 8)
Nodename: #comment (nodetype: 8)
Nodename: bookstore (nodetype: 1)

Mozilla (Firefox) output:

Nodename: #comment (nodetype: 8)
Nodename: #comment (nodetype: 8)
Nodename: bookstore (nodetype: 1)

Document object reference manual