只需检索xml文件的第一个结果

时间:2012-06-21 16:50:09

标签: android xml-parsing sax

我正在尝试检索搜索的第一个结果。搜索返回一个xml文件,我正在使用sax解析器处理该xml。 事实上,我想解析xml文件,并在我处理该搜索的第一个结果时停止。

ie:我想解析这个文件:http://musicbrainz.org/ws/2/release?query=barcode:606949062927并只检索第一个结果(来自Eminem的专辑)。但是当我使用我的代码时,我存储了最后的结果(Daniel Swain的专辑),这是我不想要的。因此,如果我可以在第一个结果之后停止解析,那么这将会更好(耗时更少)。

我认为这可以通过设置布尔值firstResult来实现,并在我完成处理第一个结果时将其设置为true,但我觉得它很脏。你认为还有另一种方法吗?

以下是我的代码的一部分:

public DataMusic parseBarcode(InputStream is) {

    final DataMusic resultAlbum = new DataMusic();

    RootElement root = new RootElement(ATOM_NAMESPACE,"metadata");
    Element releaseList=root.getChild(ATOM_NAMESPACE,"release-list");
    Element release = releaseList.getChild(ATOM_NAMESPACE,"release");
    Element title = release.getChild(ATOM_NAMESPACE,"title");
    Element artistCredit = release.getChild(ATOM_NAMESPACE,"artist-credit");
    Element nameCredit = artistCredit.getChild(ATOM_NAMESPACE,"name-credit");
    Element artist = nameCredit.getChild(ATOM_NAMESPACE,"artist");
    Element artistName = artist.getChild(ATOM_NAMESPACE, "name");
    releaseList.setStartElementListener(new StartElementListener() {
        @Override
        public void start(Attributes attributes) {
            if(attributes.getValue("count").equals(null) || attributes.getValue("count").equals("") || attributes.getValue("count").equals("0")) {
                resultAlbum.setMBId(null);
                resultAlbum.setAlbumName(null);
                resultAlbum.setArtistName(null);
                Log.w(TAG_MB, "Album was not found on MB. count=" + attributes.getValue("count") +".");
            }

        }
    });
    release.setStartElementListener(new StartElementListener() {
        @Override
        public void start(Attributes attributes) {
            resultAlbum.setMBId(attributes.getValue("id"));
            Log.v(TAG_MB, "Album found with id=" + attributes.getValue("id"));
        }

    });
    title.setEndTextElementListener(new EndTextElementListener() {
        @Override
        public void end(String body) {
            Log.v(TAG_MB, "Album name set to " + body);
            resultAlbum.setAlbumName(body);
        }

    });
    artistName.setEndTextElementListener(new EndTextElementListener() {
        @Override
        public void end(String body) {
            Log.v(TAG_MB, "Album artist name set to " + body);
            resultAlbum.setArtistName(body);
        }           
    });
    try {
        Log.v(TAG_MB, "Starting parsing data...");
        Xml.parse(is, Xml.Encoding.UTF_8, root.getContentHandler());
        Log.v(TAG_MB, "Finished parsing. Returning album with MBID=" + resultAlbum.getMBId() + " and name=" + resultAlbum.getAlbumName()) ;
        return resultAlbum; 
    } catch (SAXException e) {
        Log.e(TAG_MB + "_SAXException", "SAXException with MusicBrainz");
        Log.e(LOGCAT_TAG + "_SAXException",e.getMessage());
    } catch (IOException e) {
        Log.w(TAG_MB+ "_IOException", "The HTTP Request wasn't good. Either 403 error, or the album wasn't found on LASTFM");
        Log.w(LOGCAT_TAG+ "_IOException", "" + e.getMessage(), e);
    }
    return null;

}

1 个答案:

答案 0 :(得分:0)

我认为唯一比拥有布尔firstResult更好的是拥有DataMusic个对象的单个实例。您可以在函数顶部对应用程序范围的单实例DataMusic对象进行空检查。如果你发现它null,你可以确定第一次发生XML的解析,你可以继续初始化并设置对象的值。下次当您检查并发现DataMusic的单实例对象不为空时,您可以得出结论,XML已经被解析并且已经设置了值。

希望这个解释有所帮助。

相关问题