Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

NodeList and NamedNodeMap

We have encountered two other interesting interfaces, but we have not yet examined them in detail—NodeList and NamedNodeMap. These two interfaces exist to process lists of nodes and are surprisingly similar. NodeList is the simpler of the two, and we will start with it.

NodeList

NodeList has two methods of interest:

  • int getLength() Returns the count of nodes within the list

  • Node item(int index) Returns the Node at index

Otherwise, a NodeList is straightforward. One other important aspect of a NodeList is that the Nodes are returned in the order in which they are specified in the XML or in the order they were specified when added to the parent node.

NamedNodeMap

NamedNodeMap is similar to NodeList and, in fact, contains getLength() and item methods exactly like NodeList. However, a NamedNodeMap has a different purpose, namely to handle and manipulate lists of nodes. Attributes of a Node, for example, would be handled by a NamedNodeMap. While NamedNodeList and NodeList have similar methods, they are not otherwise related. NamedNodeLists are not maintained in any particular order or via any particular representation.

NamedNodeLists contain three other methods:

  • Node getNamedItem(String name) Returns a node representing name.

  • Node removeNamedItem(String name) Removes the node represented by name. The removed node is returned.

  • Node setNamedItem(Node name) Inserts or replaces the given node into the NamedNodeList. If the node had name foo and value bar, it would later be accessible via getNamedItem(`foo').

  • + Share This
  • 🔖 Save To Your Account