April 13, 2011, 11:33 p.m.
posted by equivalent
The DOM Traversal Module
The examples in Chapters 9 to 11 duplicated quite a bit of tree-walking code. Some of them searched for particular information. Others modified documents in memory. What they all had in common was that they navigated a tree from the root to the deepest leaf element in document order. This is an extremely common pattern in DOM programs.
The org.w3c.dom.traversal package is a collection of utility interfaces that implement most of the logic needed to traverse a DOM tree. These include NodeIterator, NodeFilter, TreeWalker, and DocumentTraversal. DOM implementations are not required to support these interfaces, but many do, including the Oracle XML Parser for Java and Xerces. (Crimson does not. GNU JAXP supports NodeIterator but not TreeWalker.) By reusing these classes you can simplify your programs a great deal and save yourself a significant amount of work.