1

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.

james.garriss
  • 12,959
  • 7
  • 83
  • 96
user3089869
  • 209
  • 2
  • 6
  • 14

2 Answers2

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
  • 1
    Just 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