I have a unique situation where my XML files (that must be parsed and read into an
org.w3c.dom.Document object) will always: (1) consist solely of hierarchial, nested tags/elements (no comments, prologs, character data, etc.), and (2) need to be traversed in a post-order fashion; meaning that all of a Node's children will be visited/processed before the Node itself.
Looking at the API, I see all sorts of functions like:
Element getFirstChild()
Element getNextSibling()
Element getParent()
// etc...
The problem is that these only help me migrate to a specific adjacent node; I can't seem to find a built-in (Java SE 6) post-order traversal mechanism, where I can just specify post-order as the traversal type, and call, say, a
getNext() method.
I'm looking for something similar to:
Document doc = docBuilder.parse("example.xml");
Tree<Element> traverser = new Tree<Element>(Tree.POST_ORDER);
traverser.setRoot(doc.getRoot());
Element temp;
while(!traverser.hasNext())
{
temp = traverser.getNext();
// do something with the current Node
}
Of course, the above code is bogus, as I'm just trying to show an example of what I need. Does anybody know how this can be accomplished in the Java DOM?