- Defining the Document Object Model
- DOM Core Level I
- Creating Document Objects
- Node Interface
- NodeList and NamedNodeMap
- Document Interface
- Element Interface
- Attr Interface
- Additional Interfaces
- Creating DOM Elements
- DOM Level II
- The DOM Core Defined
- Implementation Anomalies
- Suggested for Further Study
- Further Reading
NodeList and NamedNodeMap
We have encountered two other interesting interfaces, but we have not yet examined them in detailNodeList 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 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 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').