Search code examples
javaxmlxmldog

org.xml.sax.SAXParseException; Content is not allowed in prolog


I had this exception

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 39; Content is not allowed in prolog.
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:188)

when using XMLDog sniff() method.

My Java code is

String xml = "..."
XPathResults results = xmlDog.sniff(new InputSource(new StringInputStream(xml)));

as you can see I have my XML in String, so there is no problem with UTF-8 BOF, also I'm sure that there are no whitespaces in xml string...


Solution

  • The reason I found later was, that I used StringInputStream class and didn't realize, that it is org.hsqldb.lib.StringInputStream.StringInputStream(String).

    When I replaced this one with

    • java.io.StringBufferInputStream.StringBufferInputStream(String) // deprecated
    • or java.io.StringReader.StringReader(String) // sniff accepts reader too
    • or java.io.ByteArrayInputStream.ByteArrayInputStream(byte[])

    ...it worked. I didn't investigate why the previous one from hsqldb package is bad in this case...