android xmlpullparser检测到每个END_TAG两次

时间:2016-06-18 15:47:59

标签: android xml xmlpullparser end-tag

我真的在搜索这个,但找不到答案,我的情况并非如此 似乎离我所读过的文档和例子太远了,但是...... 我只是解析一个XML文件,嵌套元素与xmlpullparser,和 将数据存储在自定义对象的ArrayList中 几乎所有东西都可以,但我最终得到了2x太多的物体...... 从我收集的到现在,问题来自a 每次END_TAG事件发生后都会触发START_TAG事件,然后解析继续,我会重新遇到{I}正常看到的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循环... 我完全迷失了为什么会这样......

1 个答案:

答案 0 :(得分:0)

只需在每个break;

之后添加case即可