android xmlpullparser 检测每 END_TAG 两次

android xmlpullparser detects every END_TAG twice

我真的搜索过这个,但找不到答案,我的情况没有 似乎与我阅读过的文档和示例相去甚远,但是...
我只是在解析一个 XML 文件,其中包含带有 xmlpullparser 的嵌套元素,并且 将数据存储在自定义对象的 ArrayList 中。
几乎一切都很好,但最后我得到了 2 倍太多的对象...... 从我收集到现在的情况来看,问题在于 END_TAG 事件每次都在 START_TAG 事件之后触发,然后解析继续,我在正常应该看到的地方再次遇到相同的 END_TAG

这是我的解析循环:

int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    String name;
    switch (eventType) {
        case XmlPullParser.START_DOCUMENT:
            Log.i(TAG, "getLoadedXmlValues __ START_DOCUMENT");
            shop = new ArrayList();
            break;
        case XmlPullParser.START_TAG:
            name = parser.getName();
            Log.i(TAG, "getLoadedXmlValues __ START_TAG name = " + name);
            if (name.equalsIgnoreCase("WELCOME_TEXT")) {
                waitingScreenText = parser.nextText();
                currentContent = waitingScreenText;
            } else if (name.equalsIgnoreCase("ARTIST")) {
                //Log.i(TAG, "getLoadedXmlValues __ creating new artist object");
                currentArtist = new Artist();
            } else if (currentArtist != null) {
                if (name.equalsIgnoreCase("NAME")) {
                    currentArtist.setName(parser.nextText());
                    currentContent = currentArtist.getName();
                    //Log.i(TAG, "getLoadedXmlValues __ artist object exists, setting values. NAME = " + currentArtist.getName());
                } else if (name.equalsIgnoreCase("PICTURE")) {
                    currentArtist.setPicture(parser.nextText());
                    currentContent = currentArtist.getPicture();
                } else if (name.equalsIgnoreCase("DESCRIPTION")) {
                    currentArtist.setDescription(parser.nextText());
                    currentContent = currentArtist.getDescription();
                } else if (name.equalsIgnoreCase("TATTOOS")) {
                    currentTattoos = new ArrayList<>();
                } else if (name.equalsIgnoreCase("TATTOO")) {
                    currentTattoos.add(parser.nextText());
                    currentContent = "adding a tattoo";
                } else if (name.equalsIgnoreCase("DRAWINGS")) {
                    currentDrawings = new ArrayList<>();
                } else if (name.equalsIgnoreCase("DRAWING")) {
                    currentDrawings.add(parser.nextText());
                    currentContent = "adding a drawing";
                }
                Log.i(TAG, "getLoadedXmlValues __ START_TAG  content = " + currentContent);
            }
        case XmlPullParser.END_TAG:
            name = parser.getName();
            Log.i(TAG, "getLoadedXmlValues __ END_TAG name = " + name);
            if (name.equalsIgnoreCase("TATTOOS")&& currentArtist !=null&& currentTattoos !=null) {
                currentArtist.setTattoos(currentTattoos);
            } else if (name.equalsIgnoreCase("DRAWINGS")&& currentArtist !=null&& currentDrawings !=null) {
                currentArtist.setDrawings(currentDrawings);
            } else if (name.equalsIgnoreCase("ARTIST")&& currentArtist !=null) {
                //Log.i(TAG, "getLoadedXmlValues __ END_TAG :: add artist to shop");
                shop.add(currentArtist);
            }
    }

这是我的 xml : (我从网上下载了一个文件,然后写下了它的内容 在内部存储的文件中 builder.append(line).append("\n"); )

<?xml version="1.0" encoding="utf-8"?>
<datas>
    <DATE_CREATED_TIMESTAMP>
        123456789
    </DATE_CREATED_TIMESTAMP>
    <WELCOME_TEXT>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent venenatis lacus ac est ultrices dictum. Aenean rhoncus finibus turpis, sit amet condimentum augue porta eget.
    </WELCOME_TEXT>
    <ARTISTS>
        <ARTIST>
            <NAME>artiste 1</NAME>
            <PICTURE>http://dummy.url/artiste_1/face-1.jpg</PICTURE>
            <DESCRIPTION>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent venenatis lacus ac est ultrices dictum. Aenean rhoncus finibus turpis, sit amet condimentum augue porta eget.</DESCRIPTION>
            <TATTOOS>
                <TATTOO>http://dummy.url/artiste_1/tattoos/a.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_1/tattoos/b.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_1/tattoos/c.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_1/tattoos/d.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_1/tattoos/e.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_1/tattoos/f.jpg</TATTOO>
            </TATTOOS>
                <DRAWING>http://dummy.url/artiste_1/dessisn/a.jpg</DRAWING>
            <DRAWINGS>
                <DRAWING>http://dummy.url/artiste_1/dessisn/b.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_1/dessisn/c.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_1/dessisn/d.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_1/dessisn/e.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_1/dessisn/f.jpg</DRAWING>
            </DRAWINGS>
        </ARTIST>
        <ARTIST>
            <NAME>artiste 2</NAME>
            <PICTURE>http://dummy.url/artiste_2/face-2.jpg</PICTURE>
            <DESCRIPTION>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent venenatis lacus ac est ultrices dictum. Aenean rhoncus finibus turpis, sit amet condimentum augue porta eget.</DESCRIPTION>
            <TATTOOS>
                <TATTOO>http://dummy.url/artiste_2/tattoos/a.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_2/tattoos/b.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_2/tattoos/c.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_2/tattoos/d.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_2/tattoos/e.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_2/tattoos/f.jpg</TATTOO>
            </TATTOOS>
            <DRAWINGS>
                <DRAWING>http://dummy.url/artiste_2/dessisn/a.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_2/dessisn/b.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_2/dessisn/c.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_2/dessisn/d.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_2/dessisn/e.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_2/dessisn/f.jpg</DRAWING>
            </DRAWINGS>
        </ARTIST>
        <ARTIST>
            <NAME>artiste 3</NAME>
            <PICTURE>http://dummy.url/artiste_3/face-3.jpg</PICTURE>
            <DESCRIPTION>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent venenatis lacus ac est ultrices dictum. Aenean rhoncus finibus turpis, sit amet condimentum augue porta eget.</DESCRIPTION>
            <TATTOOS>
                <TATTOO>http://dummy.url/artiste_3/tattoos/a.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_3/tattoos/b.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_3/tattoos/c.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_3/tattoos/d.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_3/tattoos/e.jpg</TATTOO>
                <TATTOO>http://dummy.url/artiste_3/tattoos/f.jpg</TATTOO>
            </TATTOOS>
            <DRAWINGS>
                <DRAWING>http://dummy.url/artiste_3/dessisn/a.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_3/dessisn/b.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_3/dessisn/c.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_3/dessisn/d.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_3/dessisn/e.jpg</DRAWING>
                <DRAWING>http://dummy.url/artiste_3/dessisn/f.jpg</DRAWING>
            </DRAWINGS>
        </ARTIST>
    </ARTISTS>
</datas>

这是日志摘录:

 START_DOCUMENT
START_TAG name = datas
 END_TAG name = datas
 START_TAG name = DATE_CREATED_TIMESTAMP
 END_TAG name = DATE_CREATED_TIMESTAMP
 END_TAG name = DATE_CREATED_TIMESTAMP
 START_TAG name = WAITING_SCREEN
 END_TAG name = WAITING_SCREEN
 START_TAG name = WELCOME_TEXT
 END_TAG name = WELCOME_TEXT
 END_TAG name = WAITING_SCREEN
 START_TAG name = ARTISTS
 END_TAG name = ARTISTS
 START_TAG name = ARTIST
 END_TAG name = ARTIST
 START_TAG name = NAME
 START_TAG  content = artiste 1
 END_TAG name = NAME
 START_TAG name = PICTURE
[...]

如您所见,无论元素是否包含嵌套元素,开始标记总是会导致 START_TAGEND_TAG 都被触发...
我试图记录我的本地 xml 文件来检查它,它是正确的,所以问题必须来自我的 while 循环...... 我完全不知道为什么会这样...

只需在每个 case

后添加 break;