• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Count the no of a particular element in xml file

 
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Could anyone please tell me how to count the number of a particular tag in xml

e.g. a file contains <abc> tag 2 times , then is there a function to count it's no. of occurances ?


Please advise.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could load the file into a Document, then use getElementsByTagName("abc") to return a list of those elements.
 
Ranch Hand
Posts: 734
7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say, even though it is not as radically different of how to count the number of dust particles in a clean room from how to count the number of a molecule in a molar volumn or from the number of cards in a dealing shoe, the way of counting the number of tag abc is still... different for different operation context. What then is the context?
 
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The idea is to use xpath count function

1. count(//help)
This is NO xml namespace. This is OK by the method useInputSource(). The count is 1 as expected.

2. count(//mylaws:help)
This is with xml namespace. This is NOT OK by the method useDom() and selectCount(Object inputSource, String expression) { // This is for useDom()
The count is 0.

I tested with XPath-View and it return 1 as expected.

What/where/why is the issue in the method selectCount(Object inputSource, String expression) { // This is for useDom()

 
g tsuji
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maybe you should open your own thread for your own problem.
 
H Paul
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Missing code added and works.

2. In which scenario (XML Input), the following method will be called



 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

At least for my problem, I just made some changes in the java class which was generating my xml so that xml won't have the same data twice.
Why I needed the count function is that I wanted to check if there was any duplicate data in the xml.

Thank you all for your advise.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic