A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Engineering
»
XML and Related Technologies
Author
Finding maximum node depth of an xml dom tree
Swethar Kalki
Greenhorn
Joined: Mar 26, 2012
Posts: 4
posted
Apr 23, 2012 04:13:51
0
How do I find the maximum node depth of an xml file using dom in
java
?
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2407
posted
Apr 23, 2012 05:07:47
0
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: 12265
1
posted
Apr 23, 2012 08:14:52
0
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
Java Resources at
www.wbrogden.com
I agree. Here's the link:
jrebel
subject: Finding maximum node depth of an xml dom tree
Similar Threads
copy xml string into a dom tree
current-element-node property not recognized
Convert XSD to XML
Removing the XML node from a DOM object
ParserImpl for DOM
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter