Skip to content Skip to sidebar Skip to footer

Parsing The Cdata Section In Xml Using Xml Pull Parser

Sample XML NDTV News - Top Storieshttp://www.ndtv.com/

Solution 1:

You can use JSoup. Download @ http://jsoup.org/download. Add the jar to the libs folder.

To parser i copied the rss feed to xml file in assests folder. (localy)

XmlPullParser xpp = factory.newPullParser();
InputStream is = this.getAssets().open("xmlparser.xml");
xpp.setInput(is, "UTF_8");

You can use the below since you have the url. I ave shown how to extract the url and the content. you need to extract the contents of other tags as you would do normally.

XmlPullParserxpp= factory.newPullParser();

    xpp.setInput(urlStream, null);

    booleaninsideItem=false;

    // Returns the type of current event: START_TAG, END_TAG, etc..inteventType= xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {

            if (xpp.getName().equalsIgnoreCase("entry")) {
                insideItem = true;
            }
             elseif (xpp.getName().equalsIgnoreCase("content")) {
                    if (insideItem)
                    {
                        Documentdoc= Jsoup.parse(xpp.nextText());

                        Elementslinks= doc.select("a[href]"); // a with hreffor (Element link : links) {
                                Log.i("........",""+link.attr("abs:href"));
                            }

                        Elementdivcontent= doc.select("span").first();

                        Log.i("..........",""+divcontent.text());

                    }
                }
        } elseif (eventType == XmlPullParser.END_TAG
                && xpp.getName().equalsIgnoreCase("entry")) {
            insideItem = false;
        }

        eventType = xpp.next(); // move to next element
    }

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (XmlPullParserException e1) {
    e1.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
}

Log :

08-0308:03:04.413: I/........(1524): http://www.ndtv.com/news/images/topstory_thumbnail/   Shatrughan_Sinha_agency_120.jpg08-0308:03:04.423: I/..........(1524): The BJP is likely to anoint Narendra Modi as its prime ministerial candidate for the 2014 elections and make a formal announcement to that effect by September.

Edit: To loop through the elements

Elements divcontent = doc.select("span");
for(int k= 1;k<divcontent.size();k++)
{
     String spancontent =divcontent.get(k).text();
     Log.i("..........",spancontent);
}

Post a Comment for "Parsing The Cdata Section In Xml Using Xml Pull Parser"