• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

DOM Parsing

 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Is there any way using DOM API to find out how many occurences of a particular Node name are there in our XML
As I m trying to find a node having multiple occurences
but when i try to find it it stops after giving value of first node only

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gaurav -
This may not be the most elegant approach, but it should work:
Element root = doc.getDocumentElement();
NodeList nodes = root.getElementsByTagName("match");
int count = nodes.getLength();
One thing to keep in mind with this tactic is that it will match all matching descendents of the current element. For instance:
<a1>
<b1><match /></b1>
<b2><match /></b2>
<b3><match /></b3>
<b4>
<c1><match /></c2>
<c2><match /></c2>
<c3>
<d1><match /></d1>
<d2><match /></d2>
</c3>
</b4>
</a1>
This would find 7 nodes. Hope that helps.
Chris
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!