The NodeList Interface

The NodeList interface provides access to the ordered content of a node. Most frequently, it is used to retrieve text nodes and child elements of element nodes. See Table 19-4 for a summary of the NodeList interface.

Table 19-4. The NodeList interface

Name

Type

Read-only

2.0

3.0

Attribute

    

length

Long

  

Method

    

item

Node

   

The NodeList interface is extremely basic and is generally combined with a loop to iterate through the children of a node, as in the following example:

NodeList nl = nd.getChildNodes( );
for (int i = 0; i < nl.getLength( ); i++) {
  Node ndChild = nl.item(i);
  if (ndChild.getNodeType( ) =  = Node.COMMENT_NODE) {
    System.out.println("found comment: " + ndChild.getNodeValue( ));
  }
}

Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.