XML DOM Node List
Node lists are returned by methods like getElementsByTagName() and the childNodes property.
Try It Yourself - Examples
The following examples use the XML file books.xml. loadXMLDoc(), located in an external JavaScript, is used to load the XML file.
Get text from the first <title> element
Traverse nodes using the length property
DOM Node List
When using properties or methods such as childNodes or getElementsByTagName(), a node list object is returned.
The node list object represents a list of nodes, in the same order as in the XML.
Nodes in the node list are accessed using a zero-based index.
The following image represents the node list of <title> elements in "books.xml":
The following code snippet loads "books.xml" into xmlDoc using loadXMLDoc() and returns the node list of title elements in "books.xml":
After the above statement, x is a node list object.
The following code snippet returns the text from the first <title> element in the node list (x):
Example
txt = x[0].childNodes[0].nodeValue;
After the above statement, txt = "Everyday Italian".
Node List Length
The node list object keeps itself updated. If elements are deleted or added, the list updates automatically.
The length property of the node list is the number of nodes in the list.
The following code snippet loads "books.xml" into xmlDoc using loadXMLDoc() and returns the number of <title> elements in "books.xml":
After the above statement, x = 4.
The length of the node list can be used to loop through all elements in the list.
The following code snippet uses the length property to loop through the list of <title> elements:
Example
xmlDoc = loadXMLDoc("books.xml");
// x variable gets the specified node list
x = xmlDoc.getElementsByTagName('title');
for (i = 0; i < x.length; i++) {
document.write(x[i].childNodes[0].nodeValue);
document.write("");
}
Output:
Example explanation:
- Loads "books.xml" into
xmlDocusing loadXMLDoc() - Sets the
xvariable to hold the node list of alltitleelements - Outputs the value from the text node of all
<title>elements
DOM Attribute List (Named Node Map)
The attributes property of an element node returns a list of attribute nodes.
This is called a Named Node Map, similar to a node list except for some differences in methods and properties.
The attribute list keeps itself updated. If attributes are deleted or added, the list updates automatically.
The following code snippet loads "books.xml" into xmlDoc using loadXMLDoc() and returns the list of attribute nodes for the first <book> element in "books.xml":
After the above code, x.length equals the number of attributes, and x.getNamedItem() can be used to return the attribute node.
The following code snippet displays the value of the "category" attribute of a book and the number of its attributes:
Example
xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc.getElementsByTagName("book")[0].attributes;
document.write(x.getNamedItem("category").nodeValue);
document.write("" + x.length);
Output:
Example explanation:
- Loads "books.xml" into
xmlDocusing loadXMLDoc() - Sets the
xvariable to hold a list of all attributes of the first<book>element - Outputs the value of the "category" attribute
- Outputs the length of the attribute list