java parsing_error ai_generated true

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

ID: java/xml-parse-error

Also available as: JSON · Markdown
90%Fix Rate
92%Confidence
70Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
21 active

Root Cause

The Java XML parser encountered malformed content before the XML declaration or root element. The most common cause is a BOM (Byte Order Mark) at the beginning of a UTF-8 file, invisible whitespace or characters before the <?xml declaration, or the input is not XML at all (e.g., an HTML error page returned from an HTTP request, or an empty response). This error is prevalent in Spring configuration parsing, SOAP services, and Maven POM files.

generic

Workarounds

  1. 92% success Remove BOM and invisible characters from the beginning of the XML input
    Check for BOM: open the file in a hex editor and look for EF BB BF (UTF-8 BOM) or FF FE / FE FF (UTF-16 BOM) at the start. Remove BOM: save the file as UTF-8 without BOM. In code: strip leading whitespace/BOM before parsing. Use InputStreamReader with explicit charset: new InputStreamReader(stream, StandardCharsets.UTF_8). For files from Windows, ensure the editor saves without BOM.
  2. 88% success Validate that the input is actually XML before parsing
    Log or inspect the first 100 bytes of the input to see what is actually there. Common non-XML inputs: HTML error pages from a web server (check for <!DOCTYPE html>), empty responses, JSON responses, or binary data. For HTTP-sourced XML, check the Content-Type header (should be application/xml or text/xml). Add error handling to log the actual content when parsing fails.

Dead Ends

Common approaches that don't work:

  1. Switching from SAX to DOM parser to handle the malformed content 90% fail

    Both SAX and DOM parsers perform the same XML well-formedness checks. If the input is not valid XML, neither parser can handle it. DOM may give a slightly different error message but will still reject malformed content at the prolog level.

  2. Setting the parser to be lenient or non-validating 85% fail

    Non-validating mode skips DTD/Schema validation, not well-formedness checks. 'Content is not allowed in prolog' is a well-formedness error (XML 1.0 spec), not a validation error. There is no standard way to make a Java XML parser accept malformed XML at the prolog level.

Error Chain

Leads to:
Preceded by:
Frequently confused with: