Both NodeIterator and TreeWalker can simplify the traversal of nodes of interest in a document. NodeIterator presents nodes as a one-dimensional list. TreeWalker presents them as a tree. That difference aside, the behavior and purpose of these two interfaces is much the same.

Both NodeIterator and TreeWalker traverse a subtree of the nodes in the document. Exactly which nodes belong in this subtree depends on four factors:

  • The root node of the subtree (which often is not the root node or root element of the document). Only this node and its descendants will be seen by the TreeNodeIterator / Walker .

  • whatToShow , an int constant that contains bit flags identifying which types of nodes to include. The basic bit flags for these constants are available as public final static variables in the NodeFilter interface; for example, NodeFilter.SHOW_ELEMENT , NodeFilter.SHOW_COMMENT , and NodeFilter.SHOW_TEXT .

  • A NodeFilter object whose acceptNode() method returns NodeFilter.FILTER_ACCEPT , NodeFilter.FILTER_REJECT , or NodeFilter.FILTER_SKIP for each node passed to it.

  • A boolean flag that specifies whether or not to expand entity references.

Each of these is specified when the NodeIterator or TreeWalker is created by the createNodeIterator() / createTreeWalker() method in the DocumentTraversal interface. In a DOM implementation that supports the traversal module, all classes that implement the Document interface also implement the DocumentTraversal interface. This enables you to create a NodeIterator or TreeWalker over a document by casting the corresponding Document object to DocumentTraversal .

Processing XML with Java. A Guide to SAX, DOM, JDOM, JAXP, and TrAX
Processing XML with Javaв„ў: A Guide to SAX, DOM, JDOM, JAXP, and TrAX
ISBN: 0201771861
EAN: 2147483647
Year: 2001
Pages: 191 © 2008-2017.
If you may any questions please contact us: