androidxmlandroid-xmlpullparser

Android XML parser only retrieving the first item from feed


I'm creating a small app to read in the BBC f1 news feed into android and to display in a ListView. I have created the below parsing functions by following this Android Developer Tutorial.

When the items ArrayList is returned to the activity it only has one item, the most recent news story. When I log the

These functions are called after successfully retrieving the data via a async call. On the second to last line the output title is the same as the returned array item.

I have posted the logcat output to pastebin here

RSS Structure

<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        // unrequired tags (title, description, lang etc)
        <item>
            <title></title>
            <description></description>
            <link></link>
            <guid isPermaLink="false"></guid>
            <pubDate></pubDate>
            <media:thumbnail  />
            <media:thumbnail  />
        </item>
        <item>
            <title></title>
            <description></description>
            <link></link>
            <guid isPermaLink="false"></guid>
            <pubDate></pubDate>
            <media:thumbnail  />
            <media:thumbnail  />
        </item>
    </channel>
</rss>

read feed function

private static ArrayList<BBCItem> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
    //Log.d("read feed", "");
    ArrayList<BBCItem> items = new ArrayList();

    parser.require(XmlPullParser.START_TAG, ns, "rss");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        // Starts by looking for the channel tag
        if (name.equals("channel")) {
            // navigate to the item tag here
            parser.require(XmlPullParser.START_TAG, ns, "channel");
            while (parser.next() != XmlPullParser.END_TAG){
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                name = parser.getName();
                if (name.equals("item")){
                    items.add(bbcReadEntry(parser));
                }
                else {
                    skip(parser);
                }
            }
        } else {
            skip(parser);
        }
    }
    return items;
}

read individual feed item

public static BBCItem bbcReadEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, "item");
    String title = null;
    String description = null;
    String url = null;
    String pubDate = null;
    String thumbnailURL = null;
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("title")) {
            title = readString(parser, "title");
        } else if (name.equals("description")) {
            description = readString(parser, "description");
        } else if (name.equals("link")) {
            url = readString(parser, "link");
        } else if (name.equals("pubDate")) {
            pubDate = readString(parser, "pubDate");
        } else if (name.equals("media:thumbnail")) {
            //thumbnailURL = readThumbnail(parser);
            thumbnailURL = "http://news.bbcimg.co.uk/media/images/81615000/jpg/_81615988_nicorosberg.jpg";
        }
        else {
            skip(parser);
        }
    }
    Log.d("News item title", title);
    return new BBCItem(title, description, url, pubDate, thumbnailURL);
}

Any help much appreciated

Thanks


Solution

  • I never managed to get this working full. Instead I converted the XML string to JSON and manipulated that (which I'm much more comfortable with).

    This answer is how I converted it from XML to JSON.