Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Postorder Traversal of a Java DOM Tree

843834Aug 12 2010 — edited Aug 12 2010
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?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 9 2010
Added on Aug 12 2010
1 comment
244 views