Table of Contents
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.
1 2 3 4 5 6 7 8 |
!<?xml version="1.0" encoding="utf-8"?> <person> <name> Mohtashim Nawaz </name> <age> 24 </age> <prof> Software Engineer </prof> </person> |
The code to parse the file is given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package java2blog; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class XmlParser { public static void main(String[] args) { SAXParserFactory f = SAXParserFactory.newInstance(); try { SAXParser parser = f.newSAXParser(); parser.parse("sample.xml", new DefaultHandler()); } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace(); } } } |
Output:
The parser will raise the error. However, you can correct it by removing the extra characters and changing the file as given below.
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <person> <name> Mohtashim Nawaz </name> <age> 24 </age> <prof> Software Engineer </prof> </person> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package java2blog; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class XmlParser { public static void main(String[] args) { SAXParserFactory f = SAXParserFactory.newInstance(); try { SAXParser parser = f.newSAXParser(); parser.parse("sample_unknown.xml", new DefaultHandler()); } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace(); } } } |
The “sample_unknown.xml” file does not exist.
Output:
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.
Further reading:
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!