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.
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 :-)
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.
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.