我需要使用 XmlPuppParser 解析这个 XML
<lfm status="ok">
<events xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" artist="Armin van Buuren" festivalsonly="0" page="1" perPage="50" totalPages="1" total="17">
<event xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<id>3353053</id>
<title>ULTRA Buenos Aires 2013 - Dia 2</title>
<artists>
<artist>Armin van Buuren</artist>
...
<artist>Adrian de Bernardi</artist>
<artist>Manu Desrets</artist>
<headliner>Armin van Buuren</headliner>
</artists>
<venue>
<id>8778836</id>
<name>Costanera Sur</name>
<location>
<city>Ciudad de Buenos Aires</city>
<country>Argentina</country>
<street>Av. Espana 2230</street>
<postalcode/>
<geo:point>
<geo:lat>-34.61135</geo:lat>
<geo:long>-58.35838</geo:long>
</geo:point>
</location>
<url>http://www.last.fm/venue/8778836+Costanera+Sur</url>
<website/>
<phonenumber/>
<image size="small">http://userserve-ak.last.fm/serve/34/54798997.jpg</image>
....
<image size="extralarge">http://userserve-ak.last.fm/serve/252/54798997.jpg</image>
</venue>
<startDate>Sat, 23 Feb 2013 17:58:01</startDate>
<description/>
<image size="small">http://userserve-ak.last.fm/serve/34/83063099.jpg</image>
....
<image size="extralarge">http://userserve-ak.last.fm/serve/252/83063099.jpg</image>
<attendance>45</attendance>
<reviews>0</reviews>
<tag>lastfm:event=3353053</tag>
<url>http://www.last.fm/festival/3353053+ULTRA+Buenos+Aires+2013+-+D%C3%ADa+2</url>
<website>http://ultrabuenosaires.com/</website>
<tickets></tickets>
<cancelled>0</cancelled>
<tags>
<tag>electronic</tag>
</tags>
</event>
<event xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
....
</event>
</events>
</lfm>
我需要解析这些信息:标题、艺术家、城市、国家、地理、日期和描述
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String tag = parser.getName();
MyLog.d(TAG, "tag == " + tag);
if (tag.equals("title")) {
title = readTitle(parser);
MyLog.d(TAG, "tag == " + tag + ": " + title);
} else if (tag.equals("artists")) {
MyLog.d(TAG, "tag == " + tag);
} else if (tag.equals("artist")) {
if (artists == null) {
artists = readArtist(parser);
} else {
artists += ", " + readArtist(parser);
}
MyLog.d(TAG, "tag == " + tag + ": " + artists);
} else if (tag.equals("headliner")) {
artists += ", " + readHeadliner(parser);
MyLog.d(TAG, "tag == " + tag + ": " + artists);
parser.nextTag();
} else if (tag.equals("venue")) {
MyLog.d(TAG, "tag == " + tag);
} else if (tag.equals("location")) {
MyLog.d(TAG, "tag == " + tag);
} else if (tag.equals("city")) {
city = readCity(parser);
MyLog.d(TAG, "tag == " + tag + ": " + city);
} else if (tag.equals("country")) {
country = readCountry(parser);
MyLog.d(TAG, "tag == " + tag + ": " + country);
} else if (tag.equals("geo:point")) {
MyLog.d(TAG, "tag == " + tag);
} else if (tag.equals("geo:lat")) {
geoLat = readGeoLat(parser);
MyLog.d(TAG, "tag == " + tag + ": " + geoLat);
} else if (tag.equals("geo:long")) {
geoLong = readGeoLong(parser);
MyLog.d(TAG, "tag == " + tag + ": " + geoLong);
} else if (tag.equals("startDate")) {
date = readDate(parser);
MyLog.d(TAG, "tag == " + tag + ": " + date);
} else if (tag.equals("description")) {
description = readDescription(parser);
MyLog.d(TAG, "tag == " + tag + ": " + description);
} else {
skip(parser);
}
}
private String readTitle(XmlPullParser parser) throws IOException,
XmlPullParserException {
MyLog.d(TAG, "readTitle");
parser.require(XmlPullParser.START_TAG, ns, "title");
String name = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "title");
return name;
}
private String readText(XmlPullParser parser) throws IOException,
XmlPullParserException {
MyLog.d(TAG, "readText");
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws XmlPullParserException,
IOException {
MyLog.d(TAG, "skip");
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;
}
}
}
我解析所有信息,日期和描述除外。我认为我的错误是解析位置后没有找到下一个标签。您能否更改我的代码或给我教程如何使用 XmlPullParser 解析复杂的 XML?