File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Finding maximum node depth of an xml dom tree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Finding maximum node depth of an xml dom tree" Watch "Finding maximum node depth of an xml dom tree" New topic
Author

Finding maximum node depth of an xml dom tree

Swethar Kalki
Greenhorn

Joined: Mar 26, 2012
Posts: 4
How do I find the maximum node depth of an xml file using dom in java?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Mar 22, 2000
Posts: 12785
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finding maximum node depth of an xml dom tree