Home » Android » android – Why does this code keep triggering the SaxParseException : ""PI must not start with xml"?

android – Why does this code keep triggering the SaxParseException : ""PI must not start with xml"?

Posted by: admin June 15, 2020 Leave a comment


This code is used to generate a XML document from its String representation. It works fine in my small unit tests, but fails in my actual xml data. The line where it triggers is Document doc = db.parse(is);

Any ideas?

public static Document FromString(String xml)
    // from http://www.rgagnon.com/javadetails/java-0573.html
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));

        Document doc = db.parse(is);

        return doc;
    catch (Exception e)
        Log.WriteError("Failed to parse XML", e, "XML.FromString(String)");
        return null;
How to&Answers:

Thanks for your help everyone.

I discarded the <?xml version="1.0" encoding="utf-8"?> which cleared this error. Still don’t understand what the reason for this might be, but it worked nonetheless.

I went on to find one of my buffered writers (when extracting from a zip file into memory) wasn’t being flushed, which was causing the xml string to be incomplete.

Thanks everyone for your help!


You may check if your xml file has BOM header


I had the same problem while parsing XML generated by PHP.
After I added the ContentType header “text/xml” it works like a charm.


as @StaxMan said, remove any unknown characters before

responseBody = responseBody.substring(responseBody.indexOf(“<“));


this issue will be caused too by having the line < ?xml version=”1.0″ encoding=”UTF-8″?> together with the xml data in the same line…

< ?xml version=”1.0″
< secciones>< seccion>< id>0< /id>< nombre>Portada< feedURL>http://iphone.elnorte.com/libre/online07/a


You should have checked the encoding of the file instead of discarding the xml line.

I have found that my Eclipse (on Windows) had the same problem with a resource encoded as Unix-U8. After converting it to DOS-U8, the error went away.