Question

I want to parse xml from this rss feed, but I just get things like this:

"New Arrivals

Last updated:May 28 6:45PM"

I don't have enough reputation so I can just post words instead of an image. Please excuse me.

I am really a fresh man in Android, so I copied the code from this website and make some changes to parse the above xml data. I read others' questions and think that the problem is about the "channel" tag, but after some modification, nothing changed. Here is the core code:

private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
    List<Entry> entries = new ArrayList<Entry>();

    parser.require(XmlPullParser.START_TAG, ns, "rss");

    parser.next();//I add this statement to pass the channel tag but it doesn't work

    while (parser.next() != XmlPullParser.END_TAG) {
        String name = parser.getName();
       if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        // Starts by looking for the entry tag
        if (name.equals("item")) {
            entries.add(readEntry(parser));
        } else {
            skip(parser);
        }
    }
    return entries;
}

private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {

    parser.require(XmlPullParser.START_TAG, ns, "item");
    String title = null;
    String description = null;
    String link = null;
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("title")) {
            title = readTitle(parser);
        } else if (name.equals("description")) {
            description = readDescription(parser);
        } else if (name.equals("link")) {
            link = readLink(parser);
        } else {
            skip(parser);
        }
    }
    return new Entry(title, description, link);
}

I'll appreciate it if anyone could help.

Was it helpful?

Solution

The problem is in the method readFeed, and it should be:

private List<Item> readFeed(XmlPullParser parser) throws IOException, XmlPullParserException {
    List<Item> items = new ArrayList();
    parser.require(XmlPullParser.START_TAG, null, "rss"); // first start tag begin with <rss>
    parser.nextTag();
    parser.require(XmlPullParser.START_TAG, null, "channel");// second is <channel>
    while (parser.next() != XmlPullParser.END_TAG) { // if encounter </channel>, stop
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        Log.d(TAG, "start tag: " + name);
        // Starts by looking for the item tag
        if (name.equals("item")) {
            items.add(readItem(parser));
        } else {
            skipTag(parser);
        }
    }
    return items;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top