File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Counting Number of Occurences in XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Counting Number of Occurences in XML" Watch "Counting Number of Occurences in XML" New topic

Counting Number of Occurences in XML

Saurabh Agrawal
Ranch Hand

Joined: Oct 07, 2003
Posts: 244
Hi everyone,
I have a well formed XML but not a well formatted XML like :

Now my requirement is to counter number of occurents in locale node for DE and FR locale.

For Example: Number of DE occurences is : 2
Number of FRoccurences is : 1

since the XML if not formatted so i cant use GREP for following pattern : grep "<locale>DE</locale>" <filename> | wc -l

Can anyone suggest me how can i count number of occurences in such an XML file whose format can change anytime.It may happen that entire thing is one line or may be as scattered as above.

Any quick help would be appreciated.Its urgent for me and i am stuck here.Can anyone guide me what can i do in quick time.

thanks in advance to everyone,

Success is not doing extraordinary things but doing ordinary things extraordinarily well.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Use XML-aware software instead of generic tools like grep. For example you could write an XSL transformation that uses the count() function.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Plus depending on how the "line break/carriage return/white-space character" is interpretted, you might end up with only ONE line and so only one count, right?

- m

Take a Minute, Donate an Hour, Change a Life
I agree. Here's the link:
subject: Counting Number of Occurences in XML
jQuery in Action, 3rd edition