0

Let's say, we have a XML as follows:

<Parent>
    <Child1>
        <Child2>
             <Child3>
                 <ElementNode1>ABC</ElementNode1>
                 <ElementNode2>DEF</ElementNode2>
                 <ElementNode3>GHI</ElementNode3>
             </Child3>
         </Child2>
      </Child1>
</Parent>

Now if we consider, the root node is at level 1, then how to find in a java program, the elements Child1, Child2, Child3, ElementNode2 are at which level?

I have read about the parsers DOM and DOM4J; but could not find a way.

I expect the output as follows:

Level of Child1 is 2
Level of Child2 is 3
Level of Child3 is 4
Level of ElementNode1 is 5
Level of ElementNode2 is 5
ArpanMona
  • 47
  • 1
  • 7
  • 2
    So you want to know how to parse XML in Java. There are examples of this everywhere - on this site even. – Sean Bright May 30 '19 at 18:14
  • @SeanBright No, I know how to parse. But then suppose, in the given xml, we are asked to find out the element nae which is at level 'n'. Then how to proceed? I'm not getting any clue. – ArpanMona May 30 '19 at 18:19

0 Answers0