XmlPullParser: PI should not start with xml, but only on Android 2.3.3 down

I do not get errors on Android 3.0+, but only on Android 2.2, 2.3.3, when I try to parse a small XML file through XmlPullParser , the application breaks with an error:

 org.xmlpull.v1.XmlPullParserException: PI must not start with xml (position:unknown @1:5 in java.io.InputStreamReader@40568770 ) 

What is the PI mentioned in the error ???

I found out that this can lead to the first line of the XML file ( <?xml version="1.0" encoding="utf-8"?> ), But I have not found a reason why this happens on lower versions of Android.

If this is the cause of the error (first line of the XML file), how can I fix this?

Should I:
a) ask the web server administrator to change the XML? If so, what should it change in XML?
b) substring of InputStream using BufferedReader ?

Somehow I think that the second approach will cause additional delays on weak Android phones.

EDIT

I pulled the XML content from the debugger and saw that the first seems to end with \r\n , then the following characters are triggered. Does this tell you something?

And this is what the XML file looks like. It is small and there is no obvious reason why the application crashes.

 <?xml version="1.0" encoding="utf-8"?> <song> <artist>Pink Floyd</artist> <title>Shine On You Crazy Diamond</title> <picture>http://www.xxyz.com/images/image.jpg</picture> <time>Fri, 23 Nov 2012 11:22:31 GMT</time> </song> 

This is how an InputStream taken from this XML looks like (only leading characters).

Please advise!!!

+6
source share
3 answers

I had the same problem with the data file that I used in the application. Had this exception in android 2.3. * And the problem was in the UTF-8 specification, so the easier solution I found on Windows used the Notepad ++ tool, and this allowed you to convert the file encoding to UTF-8 without the specification, and that.

+1
source

After checking the source of the xml syntax, it seems that the problem is due to the byte order marker at the beginning of the response, in my case "77u /" (in base64). If you do not convert the stream to String, but parse it directly, the parser throws it correctly.

For example, instead of

 String xml = new String(xmlData, "UTF-8"); KXmlParser parser = new KXmlParser(); parser.setInput(new StringReader(xml)); 

using

 KXmlParser parser = new KXmlParser(); parser.setInput(new ByteArrayInputStream(xmlData), null); 

Alternatively, you can also fine-tune to the first '<'

+1
source

The same problem was detected.

For me, I replaced everything with '\n' space ' ' and then it worked.

PS:

@ Amad said that an empty line before this could also cause this problem, it is better to check the first character '<' of the xml line.

0
source

All Articles