Question

EDIT 1:

I've started experimenting with XmlPullParser, and I'm thinking to handle dicts within dicts(That does sound wrong, I know) I could use recursion like readDict(XmlPullParser), and call it whenever I encounter a dict element.

This could work with a simple dict model class containing a List of HashMap objects.

I will keep this question updated as I progress...

ORIGINAL QUESTION:

Is there an android library that enables the parsing of XML in the following form?

<dict>
   <key>..</key>
   <string>..</string>
   <key>
   <array>
      <dict>
         <key>
         <string>
         <key>
         <array>
            <dict>
               <key>
               <string>
            </dict>
            <dict>...</dict>
            <dict>...</dict>
         </array>
      </dict>
   </array>
</dict>

The full XML file can be found at: http://www.tsn.ca/config/iphone/TSNiphoneconfig.xml

Problem:

As you can see, some tags are embedded "within" themselves. This is first time I'm seeing this.

I have looked into SAX Parser Factory, XML Pull Parser and the ** Android SIMPLE library**. What I'm really looking for is a library I can use to parse the XML much like SIMPLE and allow me to create data model files that match the XML elements.

However, if I can't use a library, how would I go about parsing this kind of unique XML, with XML Pull Parser for example..?

Was it helpful?

Solution

So the best solution to this problem that I could come up with is to simply use recursion. Write methods to parse each tag separately, and pass in the XMLPullParser as a parameter.

So here's some pseudocode:

parseArray(XmlPullParser xmlpp)
{
    // require start and end tags 
    if you hit another tag, call its corresponding method
}

That way if you're in parseDict and hit a dict tag, you can simply call parseDict() again.

As long as you're grabbing your data and setting it into some kind of data model, it won't matter that dict is embedded within dict repeatedly, because your data model will work either way.

OTHER TIPS

If u simply want to parse data inside 'dict' (I mean embedded 'dict' is no dependent on parent 'dict'), then u have no need of recursion logic.U can simply do like this:

int eventType = xpp.getEventType(); //xpp is reference of XmlPullParser
while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                if (xpp.getName().equalsIgnoreCase("dict")) {
                    //do something
                }
eventType = xpp.next();
            }

Whenever a 'dict' will be parsed, it will come inside same loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top