3

After multiple hours of searching and debugging I'm still stuck at the same place and Eclipse is not helping me.

I trying to parsing this rss feed

which is pretty simple. The Connection is done and I transform the InputStream as a String this works well.

When I try to parse the String obtained, I have no errors or warnings but the values i try to have are null. Here is the XML process

public class XmlOperations {
private static final String ns = null;

public List parse(String in) throws XmlPullParserException, IOException {
    try {

        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(new StringReader(in));
        parser.nextTag();
        return readFeed(parser);
    } finally {
        // 
    }
}

private List readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
    List entries = new ArrayList();
    // first xml balise
    parser.require(XmlPullParser.START_TAG, ns,"rss"); 
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        Log.w(TAG, name); // output channel the first time
        // Starts by looking for the item tag
        if (name.equals("item")) {
            **Log.w(TAG, "Never get in here" )**
          entries.add(readEntry(parser));
        } else {
            skip(parser);
            Log.w(TAG, "lala");
        }
    }  
    return entries;
}



// Parses the contents of an item. If it encounters a title, description, or link tag, hands them off
// to their respective "read" methods for processing. Otherwise, skips the tag.
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);
}

// Processes title tags in the feed.
private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "title");
    String title = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "title");
    return title;
}

// Processes link tags in the feed.
private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "link");
    String link = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "link");
    return link;
}

// Processes summary tags in the feed.
private String readDescription(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "description");
    String description = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "description");
    //Log.d("vALUE = ", summary);
    return description;
}

// For the tags title and description, extracts their text values.
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
    String result = "";
    if (parser.next() == XmlPullParser.TEXT) {
        result = parser.getText();
        parser.nextTag();
    }
    return result;
}

private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
    if (parser.getEventType() != XmlPullParser.START_TAG) {
        throw new IllegalStateException();
    }
    int depth = 1;
    while (depth != 0) {
        switch (parser.next()) {
        case XmlPullParser.END_TAG:
            depth--;
            break;
        case XmlPullParser.START_TAG:
            depth++;
            break;
        }
    }
 }

I suppose error is in one of the parsing methods.

In antoher class I have this

XmlOperations xml = new XmlOperations();
        try {
            entrieses = xml.parse(result);
            String lol = null;
            for(Entry item : entrieses) {
                //Toast.makeText(getApplicationContext(), item.title, Toast.LENGTH_SHORT).show();
                lol = item.link.toString();
            }
            tv.setText("Pin " + lol + " gouin");

entreses is a List of Entry. And still equals null :(

EDIT: With the log cat I have channel lala and that's all

4
  • Can you show a sample of the output? Might also be a good idea to quote some of the current rss feed data, in case it goes away or changes. Commented Nov 16, 2012 at 15:32
  • There are no outputs in LogCat in error or warning. The output of the Textview is Pin null gouin Commented Nov 16, 2012 at 15:36
  • can u try set a breakpoint to check if your entries.add does add the parsed items? Or you can simply add some logcat out put to log what the parser is doing. Commented Nov 16, 2012 at 15:55
  • Ok guys, I think you headed me right. When I log in the readFeed method my first item is <channel> so go in the skip(parser) method but then when i should iterate to the next xml item it doesn't. I 'll modify my code so you can see. Commented Nov 16, 2012 at 16:48

2 Answers 2

1

I finally find out. My code was right, my XML comprehension wasn't. The XML goes like this

<rss>
    <channel>
        <item>
            <link> </link>
            <title> </title>
        </item>
    </channel>
</rss>

My code went to channel and then next channel, but it doesn't exist. I just had to add parser.Next() in my reedFeed() function just before the loop to pass the channel tag and go directly to the item tag. And it works :)

Sign up to request clarification or add additional context in comments.

Comments

0

I think the line in your readFeed() function needs to be while (parser.next() != XmlPullParser.END_DOCUMENT) { instead of while (parser.next() != XmlPullParser.END_TAG) {, because otherwise it is returning nearly right away.

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.