我目前正在为 XML 文件创建解析器,一切正常,直到我添加一个额外的选项来检索链接。我有几个同名的标签,我想要一个具有特定属性值的特定标签。
...
<artist>
<name>Venom</name>
<mbid>5ddddef1-fd5a-4ca8-8e89-df4adff4239b</mbid>
<url>Venom</url>
<image size="small">http://userserve-ak.last.fm/serve/34/35628151.png</image>
<image size="medium">http://userserve-ak.last.fm/serve/64/35628151.png</image>
<image size="large">http://userserve-ak.last.fm/serve/126/35628151.png</image>
<image size="extralarge">http://userserve-ak.last.fm/serve/252/35628151.png</image>
</artist>
...
我正在寻找“超大”中的链接。
我当前的代码:
...
private TrackInfo readTrack(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "track");
String artist = null;
String artistPic = null;
String artisMbid = null;
String song = null;
String album = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("artist")) {
parser.require(XmlPullParser.START_TAG, ns, "artist");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String artistName = parser.getName();
if (artistName.equals("name")) {
artist = readArtist(parser);
} else if (artistName.equals("mbid")) {
artisMbid = readArtistMbid(parser);
} else if (artistName.equals("image")) {
artistPic = readArtistPic(parser);
} else {
skip(parser);
}
}
} else if (name.equals("name")) {
song = readSong(parser);
} else if (name.equals("album")) {
album = readAlbum(parser);
} else {
skip(parser);
}
}
return new TrackInfo(artist, artistPic, artisMbid, song, album);
}
...
private String readArtistPic(XmlPullParser parser) throws XmlPullParserException, IOException {
String artistPic = "";
parser.require(XmlPullParser.START_TAG, ns, "image");
String tag = parser.getName();
String relType = parser.getAttributeValue(null, "size");
if (tag.equals("image")) {
if (relType.equals("extralarge")) {
artistPic = readText(parser);
parser.nextTag();
}
}
parser.require(XmlPullParser.END_TAG, ns, "image");
return artistPic;
}
...
private String readText(XmlPullParser parser) throws XmlPullParserException, IOException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
...
错误:
E/CurrentTrackFragment logging﹕ XmlPullParserException: org.xmlpull.v1.XmlPullParserException: expected: END_TAG {null}image (position:START_TAG <image size='small'>@10:29 in java.io.InputStreamReader@52a3a014)
有人知道如何处理吗?
解决方案!
我开始使用 Raghunandan 给我的例子。最后我只简化了我的代码并添加了他建议的'check = true'。
private TrackInfo readTrack(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "track");
boolean nowPlaying = false;
String artist = null;
String artistPic = null;
String artistMbid = null;
String song = null;
String songUrl = null;
String album = null;
String albumPic = null;
int playedUts = 0;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("artist")) {
parser.require(XmlPullParser.START_TAG, ns, "artist");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String artistName = parser.getName();
if (artistName.equals("name")) {
artist = readArtist(parser);
} else if (artistName.equals("mbid")) {
artistMbid = readArtistMbid(parser);
} else if (artistName.equals("image")) {
boolean check = false;
String imagesize = parser.getAttributeValue(null, "size");
if (imagesize.equals("extralarge")) {
check = true;
}
if (check) {
artistPic = readArtistPic(parser);
} else {
parser.nextText();
}
} else {
skip(parser);
}
}
} else if (name.equals("name")) {
song = readSong(parser);
} else if (name.equals("url")) {
songUrl = readSongUrl(parser);
} else if (name.equals("album")) {
album = readAlbum(parser);
} else if (name.equals("date")) {
playedUts = Integer.parseInt(readUts(parser));
} else {
skip(parser);
}
}
return new TrackInfo(nowPlaying, artist, artistPic, artistMbid, song, songUrl,
album, albumPic, playedUts);
}
...
private String readArtistPic(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "image");
String artistPic = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "image");
return artistPic;
}
...