• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding maximum node depth of an xml dom tree  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I find the maximum node depth of an xml file using dom in java?
 
Saloon Keeper
Posts: 4081
98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it need to be DOM? I'm almost (but not quite) certain that you could write a one-line XPath expression to determine that value.
 
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously you are going to have to traverse the entire document.

With DOM you would use the methods in org.w3c.dom.Node to work through the whole tree.

It would be a lot easier in SAX or StaX.

Using SAX you could increment a "depth" counter with every startElement and decrement with every endElement, tracking the highest value of course.


Do you need to also remember the deepest Node?

Bill
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!