I have found twofold treatment of XmlPullParser.END_DOCUMENT tag in Android’s built-in SAX parser. Code is simple:
String s; //actually contains XML
//blah-blah
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
xpp = factory.newPullParser();
StringReader sw=new StringReader(s);
xpp.setInput(sw);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if (eventType == XmlPullParser.START_TAG)
{
//blah-blah
}
else if(eventType==XmlPullParser.TEXT)
{
//blah-blah
}
else if (eventType == XmlPullParser.END_TAG)
{
//blah-blah
}
eventType=xpp.next();
}
If XML document basically looks like (String s):
<?xml version="1.0" encoding="utf-8"?>
<templates>
<template key="Person" name="Person">
<field key="Photo" name="Photo" type="image" hint="Press to select image"/>
</template>
</templates>
With this everything works fine. But if there are some extra characters after final tag </templates> – here goes weird side (nature of my XML is such that sometimes there can appear some litter symbols after final tag).
For nearly all Android devices (about 90%) SAX parser ignores extra characters, but in some of devices – mostly devices with ICS – SAX parser tries to parse extra characters and crashes.
So the question is: what says XML standard? Should SAX parser try to parse extra symbols after final tag? And what anyway is XmlPullParser.END_DOCUMENT – it’s either end of data or just final tag?
If there are non-whitespace characters after the end tag of the root element, the file is not well-formed XML and the parser is required to report this fact to the application.