Freego Freego - 3 months ago 20
Android Question

Stuck with XmlPullParser in Android

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

Answer

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 :)

Comments