Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding maximum node depth of an xml dom tree

 
Swethar Kalki
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?
 
Tim Moores
Bartender
Posts: 3134
50
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13077
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic