Problem

It happened when use SAX to parse a XML file.

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

Solution

This error message is always caused by the invalid XML content in the beginning element. For example, extra small dot “.” in the beginning of XML element. Any characters before the “<?xml….” will cause the “org.xml.sax.SAXParseException: Content is not allowed in prolog” error message.

Invalid XML file example

A small dot “.” before the “<?xml….

.<?xml version="1.0"?>
<company>
	<staff>
		<firstname>yong</firstname>
		<lastname>mook kim</lastname>
		<nickname>mkyong</nickname>
		<salary>100000</salary>
	</staff>
	<staff>
		<firstname>low</firstname>
		<lastname>yin fong</lastname>
		<nickname>fong fong</nickname>
		<salary>200000</salary>
	</staff>
</company>

Just delete all the weird characters before the “<?xml will get rid of this error message.

This article was posted in Java category.

Related Posts