SAX Error – Content Is Not Allowed in Prolog

This article discusses the SAX Error – Content is not allowed in prolog.

The SAX parser is the XML parsing API that you can use to process the XML files. However, while using the SAX parser, you may encounter SAX error – content is not allowed in prolog.

Sax Error Due to Invalid Text Before XML Declaration

The XML files are structured using tags. Therefore, each XML file follows specified syntax.

If you place an unknown or invalid character before the XML declaration, you will get the aforementioned error while trying to parse the file using SAX error.

Let us see an example using the following XML file.

The code to parse the file is given below.

Output:

org.xml.sax.SAXParseException; systemId: file:///home/stark/eclipse-workspace-java/java2blog/sample.xml; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.

The parser will raise the error. However, you can correct it by removing the extra characters and changing the file as given below.

Observe that this XML file does not have (!) symbol at the beginning.

Byte Order Mark (BOM) At the Beginning of the XML File

The Byte Order Mark is a special unicode character that can indicate different things. The text editors may insert the BOM character at the beginning of the file automatically.

While parsing the XML file with the BOM character inserted in the beginning, you may encounter the SAX parser error if the file is parsed as stream of characters instead of stream of bytes.

However, it might not always be the case as in the latest version of Java the SAX parser can parse the BOM character correctly.

You can add or remove the Byte Order Mark character from the file using the code as well as manually in the text editor. Most of the text editors provide options to add or remove the BOM character.

Passing a Non Existent File to Parser

If you pass a file to parser that does not exist, you shall get the SAX parser error. The same can happen if you accidentally fail to provide the correct path.

So even if the file existed, if its path is not correct, you will eventually get the parser error.

Let us see an example.

The “sample_unknown.xml” file does not exist.
Output:

java.io.FileNotFoundException: /home/stark/eclipse-workspace-java/java2blog/sample_unknown.xml (No such file or directory)

However note that in this case the only error is the FileNotFoundException rather than parser error.

Different Encoding Formats Causing the Parser Error

The difference between the file encoding format and the encoding format you pass to the parser can cause the parser error.

For instance, if your file is encoded into UTF-8 encoding and you somehow pass the UTF-16 encoding to the parser, you will end up getting the parser error. Therefore, you should always check for the file encoding before parsing it.

Conclusion

This is all about the SAX error – content is not allowed in prolog. You can read more about SAX here.

Hope you have enjoyed reading the article. Stay tuned for more such articles. Happy Learning!

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *