• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Parsing XML when tag names not know

 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to parse the following XML. In this XML tags are coming dynamically. Tag names are not confirm. Value of the tag named "strCount" gives total number of tags (str0 to str10). Also the encounter tag can appear multiple times.

I am using simple-xml for parsing. Following is the pojo I created for this, which doesn't work -

Please tell me how can I create pojo for parsing this type of xml.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A SAX parser would be able to accommodate any XML tag. It would be a bit awkward if the "strCount" tag really comes after all the "str..." tags, but not a major complication. IMO, the XML is rather badly designed, including using different tag names. Position information should not be part of the tag, but part of an attribute. What's wrong with this:



No need to have different tag names. No need to provide a counter. No need for empty elements. That's what I call a win-win-win scenario :-)
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Ulf Dittmer. The xml is designed by another programmer so I can not change it. I did search about SAX parser but not getting any good example for parsing xml with DYNAMICALLY generated tags. Can you please give me some example how to parse xml using SAX parser when I don't know the tag names.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The xml is designed by another programmer so I can not change it.


You can't, but he can - talk to him to find a mutually agreeable solution.
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:You can't, but he can - talk to him to find a mutually agreeable solution.


I talked but he refused to change it. I have just one option to use this xml only.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I did search about SAX parser but not getting any good example for parsing xml with DYNAMICALLY generated tags.


It's not fundamentally different. If you look at the parser in http://www.beingjavaguys.com/2013/06/read-xml-file-with-sax-parser.html, it watches for specific tags "firstName", "email" etc. So where it has code like

you need to handle the tags with fixed names, and in a loop the ones that have varying names, like this:


The very least the other developer needs to do is to move the "strCount" tag before all the "strN" tags, so that you know which value to use for the "max" variable I used. Or maybe there is an intrinsic limit, like you know that it can never be more than 20 or so - then you can just let the loop run from 0 to 20 every time.
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic