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:
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 . |