public class NodeWalker extends Object
A utility class that allows the walking of any DOM tree using a stack instead of recursion. As the node tree is walked the next node is popped off of the stack and all of its children are automatically added to the stack to be called in tree order.
Currently this class is not thread safe. It is assumed that only one thread
will be accessing the NodeWalker
at any given time.
Constructor and Description |
---|
NodeWalker(Node rootNode)
Starts the
Node tree from the root node. |
Modifier and Type | Method and Description |
---|---|
Node |
getCurrentNode()
Return the current node.
|
boolean |
hasNext() |
Node |
nextNode()
Returns the next
Node on the stack and pushes all of its
children onto the stack, allowing us to walk the node tree without the use
of recursion. |
void |
skipChildren()
Skips over and removes from the node stack the children of the last node.
|
public NodeWalker(Node rootNode)
Node
tree from the root node.rootNode
- public Node nextNode()
Returns the next Node
on the stack and pushes all of its
children onto the stack, allowing us to walk the node tree without the use
of recursion. If there are no more nodes on the stack then null is
returned.
Node
on the stack or null if there isn't
a next node.public void skipChildren()
Skips over and removes from the node stack the children of the last node. When getting a next node from the walker, that node's children are automatically added to the stack. You can call this method to remove those children from the stack.
This is useful when you don't want to process deeper into the current path of the node tree but you want to continue processing sibling nodes.
public Node getCurrentNode()
public boolean hasNext()
Copyright © 2015 The Apache Software Foundation