IntelliJ Java IDE
The moose likes XML and Related Technologies and the fly likes Count the no of a particular element in xml file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply Bookmark "Count the no of a particular element in xml file" Watch "Count the no of a particular element in xml file" New topic
Author

Count the no of a particular element in xml file

pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 268

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.


Thanks,
Pramod
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 13842

You could load the file into a Document, then use getElementsByTagName("abc") to return a list of those elements.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 225
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?
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 228
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

Joined: Jan 18, 2011
Posts: 225

Maybe you should open your own thread for your own problem.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 228
1. Missing code added and works.

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



pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 268

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.

 
IntelliJ Java IDE
 
subject: Count the no of a particular element in xml file
 
Threads others viewed
how to Read and write XML file with some conditions
Conversion to JSTL - Help
Modifying xml tag value using java
Change XML element using Java
How to, XSLT?
developer file tools