ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Using DOM to Traverse XML
Pages: 1, 2, 3

The TreeWalker interface
You've now seen how to write filters and traverse XML documents horizontally. An XML document can also be represented as a tree structure, and the TreeWalker interface declares methods that allow you to traverse this tree structure. Take that simple XML document again:

    <D>child of C</D>
    <E>another child of C</E>

The tree representation of this simple XML document is

The tree representation.

In the tree structure you have the concept of parent nodes and children nodes. Therefore, the TreeWalker interface provides the following methods to traverse the tree structure:

Node parentNode();
Node firstChild();
Node lastChild();
Node previousSibling();
Node nextSibling();
Node previousNode();
Node nextNode();

The create method for the TreeWalker is very similar to the create method for NodeIterator; the only differences is the name and the return value. The signature for TreeWalker's create method is

TreeWalker createTreeWalker(root, whatToShow, filter, boolean);

The return from this method can be cast into TreeWalkerImpl. You can then use the traversal methods to walk he XML document. Listing 6 shows a Java application using the TreeWalker implementation class to do just that.

Listing 6: TreeWalkerClient.java - A Java application that is traversing the tree representation of the XML document.

import org.w3c.dom.Node;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.dom.traversal.TreeWalkerImpl;
import org.apache.xerces.domx.traversal.NodeFilter;
import org.apache.xerces.dom.DocumentImpl;

public class TreeWalkerClient
  public static void main(String args[])
    if ((args == null) || (args.length < 1))
      System.out.println("Usage: java demo <filename>");

      //create an object of the Document implementation class
      DOMParser parser = new DOMParser();
      DocumentImpl document = (DocumentImpl)parser.getDocument();

      //get the root of the XML document
      Node root = document.getLastChild();

      //instantiate the filter object
      AllElements allelements = new AllElements();

      //create an object of the TreeWalker implementation class
      TreeWalkerImpl tw =
            NodeFilter.SHOW_ELEMENT, (NodeFilter)allelements, true);

      //print the elements of the TreeWalker implementation class

    catch (Exception e)
      System.out.println("error: " + e);


  //traverses the tree structure representation
  public static void printElements(TreeWalkerImpl tw)
    Node n = tw.getCurrentNode();
    for (Node child=tw.firstChild();child!=null;child=tw.nextSibling())



//filters the elements of the XML document
class AllElements implements NodeFilter
  public short acceptNode (Node n)
    if (n.getNodeType() == Node.ELEMENT_NODE)
      return FILTER_ACCEPT;
    return FILTER_SKIP;

After running this program with the bank.xml file, the output is

Results of running on bank.xml.

Notice the output is the same as the NodeIteratorClient Java application. The only difference between these programs was how the XML document was traversed.


The Document Object Model (DOM) can be used within Java applications to traverse XML documents. The DOM only specifies the interfaces that can allow navigation and manipulation of XML documents, it's left to vendors to supply implementations that do the work. This article focused on Apache Software Foundation's implementation of the DOM Level 2 Traversal-Range Recommendation.

XML documents can be represented in one of two fashions: as a flat structure or as a tree structure. The NodeIterator interface declares the methods that allow programmers to traverse a flat representation of an XML document. The TreeWalker interface declares methods that allow programmers to traverse a tree representation of an XML document. The NodeFilter interface allows programmers to create filters that select which items from the XML document are included in the logical view of the application.

Stephanie Fesler is a BEA Systems expert on implementing various Java 2EE API.

Return to ONJava.com.