Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find Level of Element in XML

 
Mike Jeya
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I need to find the Level number of a Node in a XML file.
I am not able to track that.Can anyone help me out?

for ex:

<root>
<parent1>
<child1>
<child2>
</child2>
</child1>
</parent1>

<parent2>
<child21>
</child21>
</parent2>
<parent3>
</parent3>
</root>

I have to get the level of root as 0 and parent1, parent2 as 1 and child1,child21 as 2 etc..

Thanks in Advance
 
Balaji Loganathan
author and deputy
Bartender
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Jeya:

I have to get the level of root as 0 and parent1, parent2 as 1 and child1,child21 as 2 etc..

Thanks in Advance

Have you tried position() function in XSLT (if you are using xslt)
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The position() function will not work but if you're using the DOM API, you can write a simple method like this:

Note that I didn't even compile the above, let alone write a unit test for it, so it might need some tweaking.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic