我正在尝试使用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,因此我不会消耗大量内存。
答案 0 :(得分:0)
Documentation of the MusicBrainz Web Service提到了一个可以使用的java库:musicbrainzws2-java。
我没有使用该特定库的经验,但这些库倾向于提供API以透明地请求来自MusicBrainz的实体。选择正确的终点和解析是由库完成的。
您不是第一个尝试使用带有Java的MusicBrainz Web服务的人; - )
wiki of the library有一些关于如何使用它进行搜索的示例。