Content is not allowed in prolog

Something other than markup comes before the root element.

The message, as Java reports it
Content is not allowed in prolog.

Why it happens

  • The input is not XML: an HTML error page, JSON, or an empty response with a message from a server or proxy.
  • Bytes before <?xml: a UTF-8 byte order mark read through a Reader that kept it, or a UTF-16 file read as UTF-8.
  • Text before the root element, or a string passed where Java expects a file name or URI (or the other way round).

How to fix it

Look at the first bytes Java actually receives, not the file you think it reads: log them in hex. Pass the parser an InputStream so it detects the encoding and byte order mark itself, and check the HTTP status and Content-Type before parsing a response.

Java's schema validation (javax.xml.validation.Validator, and JAXB or Spring on top of it) throws at the first error unless you set an ErrorHandler whose error() records the SAXParseException and carries on; each one has getLineNumber() and getColumnNumber(). A well-formedness error ends parsing whatever the handler does.

The same problem in xmlvalid

xmlvalid validates with libxml2, which reports it as Start tag expected, '<' not found. It checks the whole document in your browser and lists every schema error with its line and column, not only the first.

Start tag expected, '<' not found

Example

Reports the error
hello <note/>
Fixed
<note>hello</note>