0

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.

5
  • Try to change "rss" to "channel" in your parser initialization : parser.require(XmlPullParser.START_TAG, ns, "channel"); Commented May 28, 2013 at 21:08
  • I did it but this time nothing appear at all. Thank you for your help. Commented May 29, 2013 at 4:23
  • have you try debugging ? Commented May 29, 2013 at 20:27
  • Sorry, I made a mistake, "New Arrival" is post by myself, not the result of parsing. So now actually I get nothing. I think I'd better read the tutorial on android.com carefully. Commented May 30, 2013 at 15:05
  • I found the answer here: stackoverflow.com/questions/13419288/… Commented Nov 18, 2015 at 4:00

1 Answer 1

0

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.