• 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

Counting Number of Occurences in XML

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
I have a well formed XML but not a well formatted XML like :
<file>
<locale>DE
</locale><name>
allan</name><locale>
FR</locale>
<id>2</id>
<locale>DE
</locale>
</file>

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,
Saurabh
 
Marshal
Posts: 28175
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
Use XML-aware software instead of generic tools like grep. For example you could write an XSL transformation that uses the count() function.
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic