SAXParseException: Content is not allowed in prolog

The prolog is the part of an XML document before the root element: the optional <?xml ?> declaration, comments and a DOCTYPE. Text is not allowed there. The parser found characters before the first <, or a declaration that is not at the very start. Often the input is not XML at all, or something such as a header line, a BOM decoded with the wrong charset, or a file path ended up in front of it.

Seen as:

  • org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
  • [Fatal Error] :1:1: Content is not allowed in prolog.
  • System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
  • parser error : Start tag expected, '<' not found
  • The processing instruction target matching "[xX][mM][lL]" is not allowed.

Input

Settings

History

Load from URL

Common causes

1. Extra text before the document

A status line, a log prefix or response headers captured with the body put text in front of the XML. Pass the parser only the body.

Before
HTTP/1.1 200 OK
<invoice number="2026-091"/>
After
<invoice number="2026-091"/>

2. Whitespace or a blank line before <?xml

The XML declaration must be the very first thing in the file. Even a newline before it makes Java report that a processing instruction named “xml” is not allowed. Trim leading whitespace when generating the file.

Before

<?xml version="1.0" encoding="UTF-8"?>
<invoice number="2026-091"/>
After
<?xml version="1.0" encoding="UTF-8"?>
<invoice number="2026-091"/>

3. A byte-order mark read with the wrong encoding

A UTF-8 BOM decoded as Windows-1252 becomes the visible text “” before the declaration. Let the parser read the raw bytes (an InputStream rather than a Reader) so it can detect the encoding itself.

Before
<?xml version="1.0" encoding="UTF-8"?>
<invoice number="2026-091"/>
After
<?xml version="1.0" encoding="UTF-8"?>
<invoice number="2026-091"/>

4. The parser was given a file name or JSON instead of XML

Passing a path to a method that expects XML text (for example new StringReader("invoice.xml")) makes the parser read the path itself. A JSON error body from an API produces the same message.

Before
{"error": "invoice not found"}
After
<error>invoice not found</error>

5. A file saved as UTF-16 that declares UTF-8

Windows PowerShell 5 writes UTF-16 when you redirect output with >. If the declaration still says UTF-8, the parser reads past it and then fails on the next bytes, so the column can be 39 and not 1. Save the file as UTF-8 so the bytes match what it declares.

Before
<?xml version="1.0" encoding="UTF-8"?>
<!-- saved as UTF-16 by PowerShell -->
<invoice number="2026-091"/>
After
<?xml version="1.0" encoding="UTF-8"?>
<!-- saved as UTF-8 -->
<invoice number="2026-091"/>

Look at the first bytes

Most of these causes are invisible in an editor, so inspect the raw bytes. xxd file.xml | head -2 works on Linux and macOS, and in Python open(path, "rb").read(32) works anywhere. The first few bytes usually settle it:

First bytes What they mean
3C 3F 78 6D <?xm: a clean start, so look elsewhere
EF BB BF UTF-8 BOM: fine if the parser reads bytes
C3 AF C2 BB C2 BF A BOM that was mis-decoded and saved again
FF FE or FE FF UTF-16: check the declared encoding
0A or 20 Whitespace before the declaration
7B {: the input is JSON
48 54 54 50 HTTP: headers were captured with the body

When parsing fails in production, log the first 32 bytes of the input, as bytes. It costs nothing and it usually answers the question on the first occurrence.

The same inputs in other parsers

Of the common parsers, only Java’s says “prolog”. The equivalents elsewhere are:

Text before the root (Java: Content is not allowed in prolog.)

  • .NET: Data at the root level is invalid. Line 1, position 1.
  • libxml2: Start tag expected, '<' not found
  • Python expat: syntax error: line 1, column 0, or not well-formed (invalid token)

A blank line before the declaration (Java: The processing instruction target matching "[xX][mM][lL]" is not allowed.)

  • .NET: Unexpected XML declaration. The XML declaration must be the first node in the document, and no whitespace characters are allowed to appear before it.
  • libxml2: XML declaration allowed only at the start of the document
  • Python expat: XML or text declaration not at start of entity

Empty input (Java: Premature end of file.)

  • .NET: Root element is missing.
  • libxml2: Document is empty
  • Python expat: no element found

PasteKit is a little more forgiving than these on two points. It removes a real BOM and tells you so, and it warns about whitespace before the declaration and then carries on, so a file that formats here can still fail in Java.

Give the parser bytes, not a string

An XML parser is designed to work out the encoding from the BOM and the declaration, and it can only do that with the raw bytes. A String or Reader has already been decoded, possibly wrongly.

try (InputStream in = Files.newInputStream(path)) {
    Document doc = builder.parse(in);
}

For an HTTP response in Java, use HttpResponse.BodyHandlers.ofInputStream() rather than ofString(). If a string is all you have, remove a leading BOM before parsing:

if (body.startsWith("\uFEFF")) {
    body = body.substring(1);
}
Document doc = builder.parse(new InputSource(new StringReader(body)));

.NET behaves the same way: XmlDocument.LoadXml fails on a string that starts with a BOM, while Load with a stream reads it correctly.

Check the content type of an HTTP response before parsing as well. A JSON error or an HTML page returned in place of the XML will otherwise surface as a prolog error, which hides the actual failure.

Frequently asked questions

What exactly is the XML prolog?

Everything before the root element: the optional XML declaration, which must come first, followed by any comments, processing instructions and a DOCTYPE. Only markup and whitespace may appear there.

Why does the file look fine in my editor?

A BOM and some whitespace characters are invisible. Open the file in a hex viewer, or paste it into PasteKit, which reports the line and column of any text before the root element and says so when it removes a BOM.

How do I avoid this in Java?

Pass DocumentBuilder.parse a File or an InputStream rather than a String or Reader, so the parser handles the BOM and encoding declaration itself, and log the first bytes of any input that fails.

Is a BOM allowed in XML?

Yes. The XML specification permits a byte-order mark, and a parser reading raw bytes consumes it without complaint. The trouble starts when the bytes are decoded first and the mark ends up as a character in the text.

Can whitespace come before the root element?

Yes, as long as the document has no XML declaration. Whitespace before a declaration is what is forbidden, because the declaration has to be at the very start.

Related