Android Studio Java XmlPullParser如何只获取某些标记值

时间:2018-01-16 21:47:38

标签: java android xml xmlpullparser android-xmlpullparser

我在解析某些xml标签时遇到问题。我对Android开发很陌生,我仍然在努力学习基础知识。

如果这篇文章有点难以阅读,我很抱歉。

我想阅读下载的以下XML文件:

    <lfm status="ok">
    <album>
        <name>Now, Diabolical</name>
        <artist>Satyricon</artist>
        <mbid>28d51e3f-b12c-4948-b35b-c1f5aae76ed2</mbid>
        <image size="">...</image>
        <listeners>131741</listeners>
        <playcount>2876007</playcount>
        <tracks>
            <track rank="1">
                <name>Now, Diabolical</name>
                <url>...</url>
                <duration>367</duration>
                <streamable fulltrack="0">0</streamable>
                <artist>
                    <name>Satyricon</name>
                    <mbid>8eed05a5-e9a1-4dda-8b33-e354c4ecc8b6</mbid>
                    <url>https://www.last.fm/music/Satyricon</url>
                </artist>
            </track>
            <track rank="2">
                <name>K.I.N.G.</name>
                <url>https://www.last.fm/music/Satyricon/_/K.I.N.G.</url>
                <duration>216</duration>
                <streamable fulltrack="0">0</streamable>
                <artist>
                    <name>Satyricon</name>
                    <mbid>8eed05a5-e9a1-4dda-8b33-e354c4ecc8b6</mbid>
                    <url>https://www.last.fm/music/Satyricon</url>
                </artist>
            </track>
        </tracks>
        <tags>
            <tag>
                <name>black metal</name>
                <url>https://www.last.fm/tag/black+metal</url>
            </tag>
            <tag>...</tag>
            <tag>...</tag>
        </tags>
    </album>
</lfm>

我用我的XmlPullParser类阅读它:

try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xpp = factory.newPullParser();

            FileInputStream fis = ctx.openFileInput("AlbumInfo.xml");

            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

            xpp.setInput(reader);

            int eventType = xpp.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagName = xpp.getName();

                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        if (tagName.equalsIgnoreCase("track")) {
                            currentXmlAlbumTrack = new XmlTracks();
                        }
                        break;

                    case XmlPullParser.TEXT:
                        curText = xpp.getText();
                        break;

                    case XmlPullParser.END_TAG:
                        if (tagName.equals("track")) {
                            xmlTrackItems.add(currentXmlAlbumTrack);
                        } else if (tagName.equals("name")) {
                            currentXmlAlbumTrack.setTrackName(curText);
                            Log.d("tag","name: " + curText);
                        } else if (tagName.equalsIgnoreCase("url")) {
                            //TODO
                        } else if (tagName.equalsIgnoreCase("duration")){
                            //TODO
                        }
                        break;

                    default:
                        break;

                }
                eventType = xpp.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

我只想解析每个name标记内的track标记。

基本上我想将所有轨道名称(其中8个)解析为ListView,但是当我使用currentXmlAlbumTrack.setTrackName(curText);时,它会尝试输出XML文件中的每个名称标签,如下所示:

D/tag: name text: Now, Diabolical
D/tag: name text: Now, Diabolical
D/tag: name text: Satyricon
D/tag: name text: K.I.N.G.
D/tag: name text: Satyricon
D/tag: name text: The Pentagram Burns
D/tag: name text: Satyricon
D/tag: name text: A New Enemy
D/tag: name text: Satyricon
D/tag: name text: The Rite of Our Cross
D/tag: name text: Satyricon
D/tag: name text: That Darkness Shall Be Eternal
D/tag: name text: Satyricon
D/tag: name text: Delirium
D/tag: name text: Satyricon
D/tag: name text: To the Mountains
D/tag: name text: Satyricon
D/tag: name text: black metal
D/tag: name text: albums I own
D/tag: name text: Black n Roll
D/tag: name text: Norwegian Black Metal
D/tag: name text: metal

有没有办法在不完全重写解析器的情况下避免这种情况?

2 个答案:

答案 0 :(得分:0)

您可以尝试使用 getDepth() https://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html#getDepth()

它返回元素的深度,因此对于您感兴趣的名称,深度应为4,因为根深度为1.在最后一个大小写中,您的代码只需稍作更改< / strong> statment,用 xpp.getDepth()== 4 检查深度:

                    case XmlPullParser.END_TAG:
                    if (tagName.equals("track")) {
                        xmlTrackItems.add(currentXmlAlbumTrack);
                    } else if (tagName.equals("name") && xpp.getDepth()==4) {
                        currentXmlAlbumTrack.setTrackName(curText);
                        Log.d("tag","name: " + curText);
                    } else if (tagName.equalsIgnoreCase("url")) {
                        //TODO
                    } else if (tagName.equalsIgnoreCase("duration")){
                        //TODO
                    }
                    break;

答案 1 :(得分:0)

替代解决方案是添加在找到曲目标记时引发的标记,在跟踪标记的末尾清除并在名称处检查标签:

try {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    XmlPullParser xpp = factory.newPullParser();

    FileInputStream fis = ctx.openFileInput("AlbumInfo.xml");

    BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

    xpp.setInput(reader);

    int eventType = xpp.getEventType();

    // add flag to distinguish parent tag
    bool isTrack=false;

    while (eventType != XmlPullParser.END_DOCUMENT) {
        String tagName = xpp.getName();

        switch (eventType) {
            case XmlPullParser.START_TAG:

                // WRONG PLACE, IT SHOULD BE INSIDE IF !!!
                // isTrack=true; // <track> tag found

                if (tagName.equalsIgnoreCase("track")) {
                    currentXmlAlbumTrack = new XmlTracks();

                    isTrack=true; // <track> tag found

                }
                break;

            case XmlPullParser.TEXT:
                curText = xpp.getText();
                break;

            case XmlPullParser.END_TAG:
                if (tagName.equals("track")) {

                    isTrack=false; // </track> end tag found

                    xmlTrackItems.add(currentXmlAlbumTrack);
                } else if (tagName.equals("name") && isTrack) {
                    currentXmlAlbumTrack.setTrackName(curText);
                    Log.d("tag","name: " + curText);
                } else if (tagName.equalsIgnoreCase("url")) {
                    //TODO
                } else if (tagName.equalsIgnoreCase("duration")){
                    //TODO
                }
                break;

            default:
                break;

        }
        eventType = xpp.next();
    }
} catch (Exception e) {
    e.printStackTrace();
}