I want to list of all the leaf nodes present in an XML document. The XML is not fixed, thus the code should work for any given XML file.
Asked
Active
Viewed 2,133 times
1
-
Which of the many Java XML API's are you using or do you want to use? – fvu Dec 26 '13 at 12:05
-
Anything is fine DOM or SAX. – user3089869 Dec 26 '13 at 12:09
2 Answers
3
Find an XML parser. Those libraries will parse the XML String for you and build an Object Oriented tree of the XML nodes (called a DOM, which stands for Document Object Model). There should be definitely a method like getChildCount()
, getChildren()
or isLeaf()
.
Take a look here: Best XML parser for Java

Community
- 1
- 1

Martijn Courteaux
- 67,591
- 47
- 198
- 287
-
1Just a small nitpick: DOM parsers build a tree, SAX parsers will just fire an event every time they find something - so for OP's use case DOM is probably easier. – fvu Dec 26 '13 at 12:10
0
If you are using the DOM:
if (!myNode.hasChildNodes())
{
// found a leaf node
}

james.garriss
- 12,959
- 7
- 83
- 96