XML DOM » Objects » NodeList

The NodeList object represents a live collection of Node objects. This means that any alterations to the number or properties of nodes is immediately reflected in the list. This list permits indexed access to individual nodes as well as iteration through the collection.


var xml_doc = new ActiveXObject("Microsoft.XMLDOM");
xml_doc.async = false;

var title_nodes = xml_doc.getElementsByTagName("title");
var n_titles = title_nodes.length
for (i = 0; i < n_titles; i++)
   document.write(title_nodes.item(i).text + "<br>");
Eyeless in Gaza
John Barleycorn

To demonstrate this Object we shall use the following simple XML file:

      <title>Eyeless in Gaza</title>
      <author>Aldous Huxley</author>
      <title>John Barleycorn</title>
      <author>Jack London</author>

We shall now create a NodeList object of all the 'title' elements using the document's getElementsByTagName method. The number of nodes in the collection is determined using the length property, and their nodeValue properties are displayed by accessing each in turn through the item method.

Language(s): JavaScript



Syntax: object.length

This property returns the number of items in the NodeList collection.


item Method

Syntax: object.item.(index)

This method returns the item at the specified index of the Node collection. These are numbered from 0 to one less than the value of the length property. Using an invalid index returns null.


Syntax: nodeList.nextNode()

This method returns the next node in the collection.


Syntax: nodeList.reset

This method resets the iterator for the collection.