我需要使用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吗?
答案 0 :(得分:0)
我使用此代码运行良好..例如使用此代码..
在解析START_TAG时,解析标记时,取一个计数器变量并将其分配1然后每当你得到标记时,在该增量处它为1.现在,在解析标记时,只检查标记是否第一次出现,如果它然后解析所需的值。
<spaces>
<space>
<name></name>
<desc></desc>
<songs>
<song>
<name></name>
<thumb></thumb>
</song>
<song>
<name></name>
<thumb></thumb>
</song>
</songs>
</space>
</spaces>
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("space")) {
cnt=1;
currentSpace = new Space();
System.out.println("space");
} else if (currentSpace != null) {
if (name.equalsIgnoreCase("name"))
{
currentSpace.setName(parser.nextText());
if(cnt==1)
{
// you have <name> from <space>
}
System.out.println(":::"+currentSpace.getName());
cnt++;
} else if (name.equalsIgnoreCase("id")) {
if (currentSpace.getId() == null) {
currentSpace.setId(parser.nextText());
}
}
}
break;