• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Find Level of Element in XML

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic