Android, Eclipse, XML files and weird error messages

More than once, I’ve gotten the following weird problem when looking at an XML layout for Android in Eclipse:

Failed to parse file C:\Eclipse_workspace\ButtonTest2\res\drawable\arrow_1.xml
Exception details are logged in Window > Show View > Error Log

The error itself isn’t exactly informative, and certainly doesn’t make sense:

org.xmlpull.v1.XmlPullParserException: PI must not start with xml (position:unknown @1:5 in
at Source)

Hmm. XML files should start with an appropriate declaration line, yet this says my XML file shouldn’t have it. That’s… weird. Normally I just remove the line til it stops complaining but as I’m playing with the second pre-release of r17 android SDK (some key components have been broken lately) and feeling bold I decided to understand this once and for all. What follows is the text of my answer on StackOverflow earlier tonight:

I’ve beaten my head against this wall a few times and now I have the answer (I *hate* mysterious behavior, especially when it seems to be random).

I just ran a 4-way test (Unix and DOS, with and without the binary “EF BB BF” BOM header/signature). I found that Eclipse does not put a binary BOM header into XML files it creates, period. If you add a file that has the `”<?xml … ?>”` declaration header, it works correctly ONLY if that binary signature is ABSENT. (Whether EOLs are Unix or DOS doesn’t matter). If you have *both* forms of header you get this problem. You can have one, the other, or neither, but never both.

So, if you create a new XML file in Eclipse you can expect (in 3.6 – 3.7.1 anyway) that it has no binary BOM header and you should properly put a `”<?xml … ?>”` declaration header in there to identify it as Unicode. If you IMPORT a project that has files with binary BOM header signatures or use an external editor that likes to add them, that’s where all this fun begins.

Ideally, ensure XML files with declaration don’t have binary BOM header before importing them… otherwise, remove that `”<?xml … ?>”` declaration header whenever Eclipse complains… short of removing the binary header that’s all you can readily do. (It’d be nice if Eclipse handled this more consistently… or at least gave you a way to identify and fix the problem readily by removing the binary BOM header…)

If in doubt, examine the file with an external hex editor, preferably one that is NOT Unicode-aware (WinHex worked for me, YMMV). Some text/hex editors will auto-detect Unicode and may auto-add a header when the file didn’t really have one.

Postscript: I use IDM’s UltraEdit / UltraCompare primarily. They work great but they’re a little too good at determining if a file is Unicode or not, to the point where these four text files ALL had the binary BOM header in hex mode!! WinHex showed the exact situation, and indeed only two of the files actually had that header. However, I’d like my regular editor to work correctly. Time to write IDM an email…

1 Comment

  1. I have the same issue, i tried deleting `”” (BOM header) in the .XLM file. But still i have the same issue. Please help me with this issue.


Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.