6

I get no error on Android 3.0+, but only on Android 2.2, 2.3.3 when I try to parse a small XML file via XmlPullParser, the app 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 PI mentioned in the error???

I found out that this may cause the first line of XML file (<?xml version="1.0" encoding="utf-8"?>), but I did not find the reason why this is happening on lower Android versions.

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

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

Somehow I think that the 2nd approach will cause extra delays on weak Android phones.

EDIT

I pulled XML content from debugger and saw that the first like is ending with \r\n, then the next characters starts. Does this say anything to you?

And this is how XML file look like. It's a small one and there is no visual reason why app is crashing.

<?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 InputStream taken from this XML look like (starting chars only).

Please advise!!!

sandalone
  • 41,141
  • 63
  • 222
  • 338
  • `I found out that this may cause the first line of XML file ()` I tought you would get this error, if it's *not* on the first line or together with your XML in one line, If so then I would contact the Admin, if that did not work then try regex.´to get it the way it should be – Ahmad Nov 23 '12 at 16:09
  • @Ahmad What does it mean "... then try regex.´to get it the way it should be"??? – sandalone Nov 23 '12 at 17:11
  • Ok, first how does the document exactly look like? Can you provide a link? – Ahmad Nov 23 '12 at 17:15
  • @Ahmad No I cannot. It's a secret location. I will copy XML here. A basic small file with one entry. – sandalone Nov 23 '12 at 17:28
  • is `` the first line or is there something before it? E.g. a linebreak? – Ahmad Nov 23 '12 at 17:37
  • @Ahmad Not according to debugger. The first element of array pulled from `InputStream` is `<`. I will attach image taken from debugger now. – sandalone Nov 23 '12 at 18:03
  • hmm... that's indeed strange. Sorry then I don't know how to fix that :/ – Ahmad Nov 23 '12 at 18:17

3 Answers3

1

I was having the same problem with a data file I have been using on an App. Was having this exception on android 2.3.* and the problem was the UTF-8 BOM, so the easier solution I found, on Windows, was use the Notepad++ tool, and that let you convert the file encoding to UTF-8 without BOM and that's it.

hmartinezd
  • 1,188
  • 8
  • 11
1

After checking the xml parser source it seems that the issue occurs due to a byte order marker on the beginning of the response, in my case '77u/' (in base64). If you don't convert the stream to String but parse it directly the parser correctly throws this away.

For example instead of

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

use

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

As an alternative you could also substring until the first '<'

slowcar
  • 314
  • 2
  • 13
0

Encountered the same problem.

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

PS:

@Ahmad said, a blank line before could also cause this problem, better to check the first character '<' of a xml string.

yjzhang
  • 4,859
  • 3
  • 19
  • 21