我真的搜索过这个,但找不到答案,我的案例似乎与我读过的文档和示例相距不远,但是......
我只是在解析一个 XML 文件,其中包含带有 xmlpullparser 的嵌套元素,并存储自定义对象的 ArrayList 中的数据。
几乎一切都还好,但最后我得到了 2x 太多的对象......从我收集到现在的内容,问题出在
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
[...]
如您所见,无论元素是否包含嵌套元素,开始标记总是会导致 theSTART_TAG
和 theEND_TAG
被触发...
我试图记录我的本地 xml 文件来检查它,它是正确的,所以问题必须来自我的while循环......我完全不知道为什么会这样......