1
votes

Currently I am working on an app that reads in and displays feeds from RSS data, I am able to parse and display the title of the article (the RSS feed is BBC news) and the description of the article, I would also like to be able to add a thumbnail to each entry.

Here is the structure of the XML:

<item> 
  <title>Major earthquake strikes Nepal</title>  
  <description>A magnitude 7.3 earthquake strikes eastern Nepal, two weeks after a devastating quake that killed more than 8,000 people.</description>  
  <link>http://www.bbc.co.uk/news/world-asia-32701385#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
  <guid isPermaLink="false">http://www.bbc.co.uk/news/world-asia-32701385</guid>  
  <pubDate>Tue, 12 May 2015 11:37:06 GMT</pubDate>  
  <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/82937000/jpg/_82937953_b060dce4-16b1-4c1f-8cb5-412a744930f9.jpg"/>  
  <media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/82937000/jpg/_82937954_b060dce4-16b1-4c1f-8cb5-412a744930f9.jpg"/> 
</item>  

I need the URL from the <media:thumbnail> tag

Here is my java code to get the title and description:

int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

                if (xpp.getName().equalsIgnoreCase("item")) {
                    insideItem = true;
                } else if (xpp.getName().equalsIgnoreCase("title")) {
                    if (insideItem)

                        title =xpp.nextText();



                } else if (xpp.getName().equalsIgnoreCase("description")) {
                    if (insideItem)
                      description = xpp.nextText();

                                         }
            } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
                insideItem = false;
                Item item = new Item(title, description, "URL HERE");

                headlines.add(item);
            }

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


        }

Item is a class that contains the title,description and URL getters and setters in order to display the data in various textviews and imageviews

1

1 Answers

0
votes

Maybe you could use something like this:

String url = "";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document document = db.parse(is);
NodeList nodeList = document.getElementsByTagName("Item");
for(int i=0; i<nodeList.getLength(); i++) {
    url = nodeList.item(i).getAttributes().getNamedItem("url").getNodeValue();
} //or if it is always only one item -> url = nodeList.item(0).getAttributes().getNamedItem("url").getNodeValue();

and then you can set the url to your Item object