Shiva Shiva - 5 months ago 16
Android Question

android xmlpullparser detects every END_TAG twice

I have really searched for this, but could find no answer, my case doesn't
seem too far from the doc and examples I have read, yet...

I am just parsing an XML file, with nested elements with xmlpullparser, and
storing the datas in an ArrayList of custom Objects.

Nearly everything's ok, but I end up with 2x too many objects at the end...
From what I have gathered till now, the problem comes from the fact that a

END_TAG
event is fired every time just after the
START_TAG
event, then the parsing continues, and I re-encounter the same
END_TAG
where I should see it normally.

Here's my parsing loop :

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);
}
}


Here's my xml : (I download a file from the web, then write its content
in a file on the internal storage with
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>


And here's a Log extract:

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
[...]


As you can see, no matter if an element contains nested elements or not, the opening tag always causes both the
START_TAG
and the
END_TAG
to be fired...

I have tried to log my local xml file to check it, and it is correct, so the problem has to come from my while loop...
I am completely lost as to why it is so...

Answer

Just add break; after each case

Comments