This is a demonstration of the use of the TreeWalker interface (part of the DOM Level 2 Traversal specification) in conjunction with the NodeFilter interface (part of the same specification). It uses a raw XML datasource, and displays the DOM tree with defined criterions:

NodeIterator Iterators are used to step through a set of nodes, e.g. the set of nodes in a NodeList, the document subtree governed by a particular Node, the results of a query, or any other set of nodes. The set of nodes to be iterated is determined by the implementation of the NodeIterator. DOM Level 2 specifies a single NodeIterator implementation for document-order traversal of a document subtree. Instances of these iterator are created by calling DocumentTraversal .createNodeIterator(). expandEntityReferences boolean The value of this flag determines whether the children of entity reference nodes are visible to the iterator. This attribute takes precedence over whatToShow and the filter. filter NodeFilter The NodeFilter used to screen nodes. root Node The root node of the NodeIterator, as specified when it was created. whatToShow unsigned long This attribute determines which node types are presented via the iterator. The available set of constants is defined in the NodeFilter interface. Nodes not accepted by whatToShow will be skipped, but their children may still be considered. Not that this skip takes precedence over the filter, if any. detach Detaches the NodeIterator from the set which it iterator over, releasing any computational resources and placing the iterator in the INVALID state. After detach has been invoked, calls to nextNode() or previousNode() will raise the exception INVALID_STATE_ERR. nextNode Returns the next node in the set and advances the position of the iterator in the set. After a NodeIterator is created, the first call to nextNode() returns the first in the set. Node The next Node in the set being iterator over, or null if there are no more members in that set. INVALID_STATE_ERR Raised if this method is called after the detach method. previousNode Returns the previous node in the set and moves the position of the NodeIterator backwards in the set. Node The previous Node in the set being iterated over, or null if there are no more members in that set. INVALID_STATE_ERR Raised if this method is called after the detach method. NodeFilter Filters are objects that know how to "filter out" nodes. If a NodeIterator or TreeWalker is given a NodeFilter, it applies the filter before it returns the text node. acceptNode Test whether a specified node is visible in the logical view of a TreeWalker or NodeIterator. This function will be called by the implementation of TreeWalker or NodeIterator; n Node The node to check to see if it passes the filter or not. short Constant to determine whether the node is accepted, rejected, or skipped, one of the three constants below. FILTER_ACCEPT Accept the node. Navigation methods defined for NodeIterator or TreeWalker will return this node. FILTER_REJECT Reject the node. Navigation methods defined for NodeIterator or TreeWalker will not return this node. For TreeWalker, the children of this node will also be rejected. NodeIterators treat this as a synonym for FILTER_SKIP. FILTER_SKIP Skip this single node. Navigation methods defined for NodeIterator or TreeWalker will not return this node. For both NodeIterator and TreeWalker, the children of this node will still be considered. TreeWalker TreeWalker objects are used to navigate a document tree or subtree using the view of the document defined by their whatToShow flags and filter (if any). Any function which performs navigation using a TreeWalker will automatically support any view defined by a TreeWalker. root Node The root node of the TreeWalker, as specified when it was created. whatToShow unsigned long This attribute determines which node types are presented via the TreeWalker. The available set of constants is defined in the NodeFilter interface. filter NodeFilter The filter used to screen nodes. expandEntityReferences boolean The value of this flag determines whether the children of entity reference nodes are visible to the TreeWalker. currentNode Node The node at which the TreeWalker is currently positioned. Alterations to the DOM tree may cause the current node to no longer be accepted by the TreeWalker's associated filter. currentNode may also be explicitly set to any node, whether or not it is within the subtree specified by the root node or would be accepted by the filter and whatToShow flags. firstChild Moves the TreeWalker to the first visible child of the current node, and returns the new node. If the current node has no visible children, returns null, and retains the current node. Node The new node, or null if the current node has no visible children in the TreeWalker's logical view. lastChild Moves the TreeWalker to the last visible child of the current node, and returns the new node. If the current node has no visible children, returns null, and reatins the current node. Node The new node, or null if the current node has no children in the TreeWalker's logical view. nextNode Moves the TreeWalker to the next visible node in document order relative to the current node, and returns the new node. If the current node has no text node, or if the search for nextNode attempts to step upward from the TreeWalker's root node, returns null, and retains the current node. Node The new node, or null if the current node has no next node in the TreeWalker's logical view. nextSibling Moves the TreeWalker to the next sibling of the current node, and returns the new node. If the current node has no visible next sibling, returns null, and retains the current node. Node The new node, or null if the current node has no next sibling in the TreeWalker's logical view. parentNode Moves to and returns the closest visible ancestor node of the current node. If the search for parentNode attempts to step upward from the TreeWalker's root node, or if it fails to find a visible ancestor node, this method retains the current position and returns null. Node The new parent node, or null if the current node has no parent in the TreeWalker's logical view. previousNode Moves the TreeWalker to the previous visible node in document order relative to the current node, and returns the new node. If the current node has no previous node, or if the search for previousNode attempts to step upward from the TreeWalker's root node, returns null, and retains the current node. Node The new node, or null if the current node has no previous node in the TreeWalker's logical view. previousSibling Moves the TreeWalker to the previous sibling of the current node, and returns the new node. If the current node has no visible previous sibling, returns null, and retains the current node. Node The new node, or null if the current node has no previous sibling in the TreeWalker's logical view. DocumentTraversal DocumentTraversal contains methods that create iterators and tree-walkers to traverse a node and its children in document order. createNodeIterator Create a new NodeIterator over the subtree rooted at the specified node. root Node The node which will be iterated together with its children. The iterator is initially positioned just before this node. The whatToShow flags and the filter, if any, are considered when setting this position. The root must not be null. whatToShow unsigned long This flag specifies which node types may appear in the logical view of the tree presented by the iterator. See the description of NodeFilter for the set of possible SHOW_ values. These flags can be combined using OR. filter NodeFilter The NodeFilter to be used with this NodeIterator, or null to indicate no filter. entityReferenceExpansion boolean The value of this flag determines whether entity reference nodes are expanded. NodeIterator The newly created NodeIterator NOT_SUPPORTED_ERR Raised if the specified root is null. createTreeWalker Create a new TreeWalker over the subtree rooted at the specified node. root Node The node which will serve as the root for the TreeWalker. The whatToShow flags and the filter, if any, are considered when setting this position. The currentNode of the TreeWalker is initialized to this node, whether or not it is visible. The "root" functions as a stopping point for traversal methods that look upward in the document structure, such as parentNode and nextNode. The root must not be null. whatToShow unsigned long This flag specifies which node types may appear in the logical view of the tree presented by the TreeWalker. See the description of NodeFilter for the set of possible SHOW_ values. These flags can be combined using OR. filter NodeFilter The NodeFilter to be used with this TreeWalker, or null to indicate no filter. entityReferenceExpansion boolean The value of this flag determines whether entity reference nodes are expanded. TreeWalker The newly created TreeWalker NOT_SUPPORTED_ERR Raised if the specified root is null.