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

This is often caused by a white space before the XML declaration, but it could be any text, like a dash or any character. I say often caused by white space because people assume white space is always ignorable, but that's not the case here.


Another thing that often happens is a UTF-8 BOM (byte order mark), which is allowed before the XML declaration can be treated as whitespace if the document is handed as a stream of characters to an XML parser rather than as a stream of bytes.

The same can happen if schema files (.xsd) are used to validate the xml file and one of the schema files has an UTF-8 BOM.


Actually in addition to Yuriy Zubarev's Post

When you pass a nonexistent xml file to parser. For example you pass

new File("C:/temp/abc")

when only C:/temp/abc.xml file exists on your file system

In either case

builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = builder.parse(new File("C:/temp/abc"));

or

DOMParser parser = new DOMParser();
parser.parse("file:C:/temp/abc");

All give the same error message.

Very disappointing bug, because the following trace

javax.servlet.ServletException
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
...
Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
... 40 more

doesn't say anything about the fact of 'file name is incorrect' or 'such a file does not exist'. In my case I had absolutely correct xml file and had to spent 2 days to determine the real problem.

Tags:

Java

Xml