解析此 XML 文件的最佳方法



我正在尝试使用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拉取解析器教程的改编,但我想避免每个节点编写太多代码,并且xml文件有很多代码。我想我更喜欢XML拉取解析器而不是DOM,这样我就不会消耗大量内存。

MusicBrainz Web Service 的文档提到了一个可以使用的 Java 库:musicbrainzws2-java。

我没有使用特定库的经验,但这些库倾向于提供一个 API 来透明地从 MusicBrainz 请求实体。选择正确的端点和解析由库完成。

你不是第一个尝试将MusicBrainz Web服务与Java一起使用的人;-)


图书馆的维基有一些关于如何使用它进行搜索的示例。

相关内容

  • 没有找到相关文章

最新更新