• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Count the no of a particular element in xml file

 
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
  • 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: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 729
7
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 729
7
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.

 
Happily living in the valley of the dried frogs with a few tiny ads.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic