Interface TreeWalker

All Superinterfaces:
TreeWalker

public interface TreeWalker extends TreeWalker
Traverse the document's nodes according to a set of parameters.

TreeWalker objects can be used to navigate a document tree or subtree using the view of the document defined by their whatToShow flags and filter (if any). The flags and filter define a view of a document (or subtree), a logical view. The methods in this interface allow to traverse that view.

Interoperability

This interface is based on the TreeWalker interface in DOM Level 4, and it does not allow setting a currentNode that is not an inclusive descendant of the root node. The old specification allows to set any node as currentNode, including nodes outside of the root subtree, but the aforementioned newer specification seems to imply, as of this writing, the opposite.

Also, this implementation behaves differently from the Xerces TreeWalker regarding attributes.

  • Method Summary

    Modifier and Type
    Method
    Description
    Moves the TreeWalker to the first visible child of the current node, and returns the new node.
    The node at which the TreeWalker is currently positioned.
    If the filter implements org.w3c.dom.traversal.NodeFilter, return it.
    Get the NodeFilter callback.
    The root node.
    int
    The bitmask specifying what types of nodes to show.
    Moves the TreeWalker to the last visible child of the current node, and returns the new node.
    Moves the TreeWalker to the next visible node in document order relative to the current node, and returns the new node.
    Moves the TreeWalker to the next sibling of the current node, and returns the new node.
    Moves to and returns the closest visible ancestor node of the current node.
    Moves the TreeWalker to the previous visible node in document order relative to the current node, and returns the new node.
    Moves the TreeWalker to the previous sibling of the current node, and returns the new node.
    void
    setCurrentNode(Node currentNode)
    Set the node at which the TreeWalker is currently positioned.

    Methods inherited from interface org.w3c.dom.traversal.TreeWalker

    getExpandEntityReferences
  • Method Details

    • getNodeFilter

      NodeFilter getNodeFilter()
      Get the NodeFilter callback.

      See NodeFilter.

      Returns:
      a reference to the NodeFilter callback.
    • getFilter

      NodeFilter getFilter()
      If the filter implements org.w3c.dom.traversal.NodeFilter, return it.

      This library's NodeFilter interface is essentially the same as the W3C interface, but the filtering constants are named differently as a reminder that the filtering behaviour of this library's implementation of NodeIterator is not exactly what was specified by W3C.

      Specified by:
      getFilter in interface TreeWalker
      Returns:
      the filter implementing the W3C's org.w3c.dom.traversal interface, or null otherwise.
    • getRoot

      DOMNode getRoot()
      The root node.

      See the description of root in DOM Level 4.

      Specified by:
      getRoot in interface TreeWalker
      Returns:
      the root node.
    • getWhatToShow

      int getWhatToShow()
      The bitmask specifying what types of nodes to show.
      Specified by:
      getWhatToShow in interface TreeWalker
      Returns:
      the bitmask to control which types of nodes are returned by the TreeWalker.
    • getCurrentNode

      DOMNode getCurrentNode()
      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 (it may also be explicitly set to such a location by setCurrentNode(Node)).

      Further traversal occurs relative to currentNode even if it is not part of the current view, by applying the filters in the requested direction; if no traversal is possible, currentNode is not changed.

      Specified by:
      getCurrentNode in interface TreeWalker
      Returns:
      the node at which the TreeWalker is currently positioned.
    • setCurrentNode

      void setCurrentNode(Node currentNode)
      Set the node at which the TreeWalker is currently positioned.

      The so-called currentNode may be explicitly set to any inclusive descendant of the root node, regardless of it being accepted by the filter and whatToShow flags. This differs from the W3C specification, where any node can be set.

      Further traversal occurs relative to currentNode even if it is not part of the logical view, by applying the filters in the requested direction; if no traversal is possible, currentNode is not changed.

      Specified by:
      setCurrentNode in interface TreeWalker
      Parameters:
      currentNode - the node to locate the TreeWalker at.
    • firstChild

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

      Specified by:
      firstChild in interface TreeWalker
      Returns:
      the new node, or null if the current node has no visible children in the logical view.
    • lastChild

      DOMNode 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 retains the current node.

      Specified by:
      lastChild in interface TreeWalker
      Returns:
      the new node, or null if the current node has no visible children in the logical view.
    • nextNode

      DOMNode 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 next node, or if the search for nextNode attempts to step upward from the TreeWalker's root node, returns null, and retains the current node.

      Specified by:
      nextNode in interface TreeWalker
      Returns:
      the next node, or null if there is no next node.
    • previousNode

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

      Specified by:
      previousNode in interface TreeWalker
      Returns:
      the previous node, or null if there is no previous node.
    • nextSibling

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

      Specified by:
      nextSibling in interface TreeWalker
      Returns:
      the next sibling or null if there is no next sibling in the logical view.
    • previousSibling

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

      Specified by:
      previousSibling in interface TreeWalker
      Returns:
      the previous sibling or null if there is no previous sibling in the logical view.
    • parentNode

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

      Specified by:
      parentNode in interface TreeWalker
      Returns:
      the new parent node, or null if the current node has no parent in the logical view.