XML DOM Node List
- Previous Page DOM Node Information
- Next Page DOM Traversing Nodes
getElementsByTagName()
methods, and childNodes
properties, a list of nodes can be returned.
DOM Node List
When using childNodes
or getElementsByTagName()
When accessing properties or methods such as this, a NodeList object will be returned.
The NodeList object represents a list of nodes, with the order the same as in the XML.
Nodes in the node list can be accessed via an index starting from 0.
The following figure illustrates books.xml Node list of the <title> element:

Assuming that "books.xml" has been loaded into the variable xmlDoc.
This code snippet returns the node list of the title elements in "books.xml":
x = xmlDoc.getElementsByTagName("title");
After executing the above statements, x becomes a NodeList object.
The following code snippet returns the text of the first <title> element in the node list (x):
Example
var txt = x[0].childNodes[0].nodeValue;
After executing the above statement, txt = "雅舍谈吃".
node list length
The NodeList object will keep its own update. If elements are deleted or added, the list will automatically update.
node list length
The attribute is the number of nodes in the list.
This code returns the number of <title> elements in "books.xml":
x = xmlDoc.getElementsByTagName('title').length;
After the above statement is executed,x
is 5.
The length of the node list can be used to loop through all elements in the list.
This code uses length
Attribute loop to traverse the list of <title> elements:
Example
x = xmlDoc.getElementsByTagName('title'); xLen = x.length; for (i = 0; i < xLen; i++) { txt += (x[i].childNodes[0].nodeValue) + " "; }
Example Explanation:
- Assuming books.xml has been loaded to
xmlDoc
Chinese - Set the variable x to save the node list of all title elements
- Get the value of the text node from the <title> element
DOM attribute list (named node map)
of element nodes attributes
Properties return a list of property nodes.
This is called a named node map, which is similar to a node list but has different methods and properties.
The attribute list will also be kept up to date. If attributes are deleted or added, the list will automatically update.
This code returns a list of attribute nodes from the first <book> element in "books.xml":
x = xmlDoc.getElementsByTagName('book')[0].attributes;
After the above code is executed,x.length
equals the number of properties, which can be used x.getNamedItem()
Returns a property node.
This code retrieves the value of the "category" attribute and the number of attributes of the first book:
Example
x = xmlDoc.getElementsByTagName("book")[0].attributes; txt = x.getNamedItem("category").nodeValue + " " + x.length;
Example Explanation:
- Assuming books.xml loaded into
xmlDoc
Chinese - Set the x variable to save the list of all attributes of the first <book> element
- Get the value of the "category" attribute and the length of the attribute list
- Previous Page DOM Node Information
- Next Page DOM Traversing Nodes