我正在尝试使用 Android 中的 Http 请求解析从以下 URL 检索到的 XML 文件:
http://www.musicbrainz.org/ws/2/recording/?query=kick%20AND%20artist:inxs
解析它的最佳方法是什么?我需要找到艺术家姓名、专辑名称和发行 ID。我一直在尝试使用此处指定的方法:
http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html
但事实证明它确实效率低下。对图书馆或我可以用来检索所需信息的更简单方法有什么建议吗?
到目前为止,我的代码如下:
public List parse(InputStream in) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
return readFeed(parser);
} finally {
in.close();
}
}
private List readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List entries = new ArrayList();
parser.require(XmlPullParser.START_TAG, ns, "metadata");
Log.i("XMLParser", "metadata");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("recording-list")) {
Log.i("XMLParser", "recording-list");
entries.add(readRecordingList(parser));
} else {
skip(parser);
}
}
return entries;
}
private Recording readRecordingList(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "recording-list");
Recording recording=new Recording(null,null,null,null);
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("recording")) {
Log.i("XMLParser", "recording");
recording = readRecording(parser);
} else {
skip(parser);
}
}
return recording;
}
private Recording readRecording(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "recording");
Recording recording=new Recording(null,null,null,null);
String albumTitle=null;
String artistName=null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
Log.i("XMLParserNodeName", name);
if (name.equals("title")) {
Log.i("XMLParser", "title");
albumTitle=readTitle(parser);
Log.i("XMLParser", albumTitle);
//recording = readRecording(parser);
}else if(name.equals("name")){
Log.i("XMLParser", "name");
name=readArtistName(parser);
Log.i("XMLParser", name);
}else {
skip(parser);
}
}
return recording;
}
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;
}
}
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
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;
}
private String readArtistName(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "name");
String name = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "name");
return name;
}
public static class Recording {
public final String recordingTitle;
public final String releaseTitle;
public final String releaseID;
public final String artistName;
public Recording(String recordingTitle, String releaseTitle,
String releaseID, String artistName) {
this.recordingTitle = recordingTitle;
this.releaseTitle = releaseTitle;
this.releaseID = releaseID;
this.artistName = artistName;
}
}
}
基本上这是对 Android 中的 XML Pull Parser Tutorial 的改编,但我想避免为每个节点编写太多代码,而 xml 文件中有很多代码。我想我更喜欢 XML Pull Parser 而不是 DOM,这样我就不会消耗大量内存。