Pages

Wednesday, 9 April 2014

Parsing XML in ADF Mobile using kXML


This sample will show you how you can parse a complex XML structure using kXML parser.

The XML data that I parse is the response received from Amazon Product Advertising API. It has the structure as shown in the attached XML. I am particularly interested in the values held by the ASIN and the Title tags, which I am pushing into a list.

A snapshot of the XML structure is shown below:


So, in the backing bean, the kXML parser code for this will look like below:

            KXmlParser parser = new KXmlParser(); //create a parser instance
            Reader stream = new StringReader(response); //response holds the XML
            parser.setInput(stream);
            parser.nextTag(); //go to first tag
            //Verify the tag name is BrowseNodeLookupResponse
            parser.require(XmlPullParser.START_TAG, null, "BrowseNodeLookupResponse");
            parser.nextTag(); //go to next tag
            //Verify the tag name is OperationRequest
            parser.require(XmlPullParser.START_TAG, null, "OperationRequest");
            parser.skipSubTree(); //not interested in these tags, so skip the whole set
            parser.nextTag(); //go to next tag
            parser.require(XmlPullParser.START_TAG, null, "BrowseNodes");
            parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, "Request");
            parser.skipSubTree();
            parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, "BrowseNode");
            parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, "BrowseNodeId");
            parser.nextText();
            parser.require(XmlPullParser.END_TAG, null, "BrowseNodeId");
            parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, "Name");
            parser.nextText(); //Name tag contains text data, so use nextText
            //Verify closing tag of Name is present
            parser.require(XmlPullParser.END_TAG, null, "Name");
            parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, "NewReleases");
            //loop through contents of NewReleases
            while (parser.nextTag() != XmlPullParser.END_TAG) {
                parser.require(XmlPullParser.START_TAG, null, "NewRelease");
                NewReleases newRelaseObj = new NewReleases();
                //loop through the NewRelelase tags
                while (parser.nextTag() != XmlPullParser.END_TAG) {
                    String tagName = parser.getName();//get the name if the tag
                    if (tagName.equals("ASIN")) {
                        String asin = parser.nextText();//contents of tag
                        newRelaseObj.setASIN(asin);//push into the list
                    } else if (tagName.equals("Title")) {
                        String title = parser.nextText();
                        newRelaseObj.setTitle(title);
                    }
                    parser.require(XmlPullParser.END_TAG, null, tagName);
                }
                newReleasesList.add(newRelaseObj);
                parser.require(XmlPullParser.END_TAG, null, "NewRelease");
            }
            parser.require(XmlPullParser.END_TAG, null, "NewReleases");
            parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, "TopItemSet");
            parser.skipSubTree();
            parser.nextTag();
            parser.require(XmlPullParser.END_TAG, null, "BrowseNode");
            parser.nextTag();
            parser.require(XmlPullParser.END_TAG, null, "BrowseNodes");
            parser.nextTag();
            parser.require(XmlPullParser.END_TAG, null, "BrowseNodeLookupResponse");
            :
            :
The explanation for the parser code is inline. Do check my other blog where this parsed XML data comes into action.

No comments:

Post a Comment