OBJECT:  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. To demonstrate this Object we shall use the following simple XML file 'library.xml'.

<library>
   <book>
      <category>fiction</category>
      <title>Eyeless in Gaza</title>
      <author>Aldous Huxley</author>
   </book>
   <book>
      <category>classics</category>
      <title>John Barleycorn</title>
      <author>Jack London</author>
   </book>
</library>

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.

Code (JavaScript):
var xml_doc = new ActiveXObject("Microsoft.XMLDOM");
xml_doc.async = false;
xml_doc.load("library.xml");

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).nodeValue + "<br>");

Output:
Eyeless in Gaza
John Barleycorn

denotes a Microsoft extension to the W3C DOM.

PROPERTIES

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

Syntax: NodeList.length

METHODS

item Method
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.item(index)

nextNode Method
This method returns the next node in the collection.

Syntax: NodeList.nextNode( )

reset Method
This method resets the iterator for the collection.

Syntax: NodeList.reset( )


Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information