SAX Error – Content is not allowed in prolog

Problem

Working XML via SAX parser, but when it parse some XML file, it prompts following error message :


org.xml.sax.SAXParseException: Content is not allowed in prolog.
	at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
	at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
	//...

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 above “org.xml.sax.SAXParseException: Content is not allowed in prolog” error message.

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>

To fix it, just delete all those weird characters before the “<?xml“.

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Preetish kumar mahyato
Guest
Preetish kumar mahyato

der is no dot in my xml den also it showing org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.

Hana Lobo
Guest
Hana Lobo

Hello!, well… I don´t understand this language :P but I have a problem with OmegaT program and the error is like this one, but… well I hope you can help me. Error:

(when I try to open my project, suddenly it doesn´t work)

¡No se ha podido leer el archivo del proyecto! (something like “The file of the project is not readable”)

javax.xml.bind.UnmarshalException
– with linked exception:
(org.xml.sax.SAXParseException; systemId: file:/Users/Ca/Documents/AS/omegat.project; lineNumber: 1; columnNumber 1; El contenido no está permitido en el prólogo.) (“content is not allowed in prolog”)

I hope you can help me :)

Thank you!

kantha
Guest
kantha

will any one help me to solve this problem ?
i parsed the html file using Jsoup jar , i am getting this line
,
instead of getting actual line like this
….what could be the problem ?

Pim Dorrestijn
Guest
Pim Dorrestijn

This error message is always caused by the invalid XML content in the beginning element << bold statement! By only switching between different JAXB implementations, you can already get this non-descriptive Exception….

Kishan Jaiswal
Guest
Kishan Jaiswal
hey man I have a xml file and I m validating that file usihng a method named issaved(File f, DomSource domsource). and I m getting an error that namd is. org.xml.sax.SAXParseException; systemId: file:/D:/dispatcherServlet-servlet.xml; lineNumber: 15; columnNumber: 67; s4s-elt-schema-ns: The namespace of element ‘beans’ must be from the schema namespace, ‘http://www.w3.org/2001/XMLSchema’. here is my class and methods public class test { public static Document getDoc(File f) { try { if(!f.exists()) { System.out.println(“No file “); return null; } DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); DocumentBuilder db=dbf.newDocumentBuilder(); Document document=db.parse(f); document.normalize(); return document; } catch(Exception ex) { System.err.println(ex); return null; } } public static void main(String[] var) {… Read more »
Faisal Basra
Guest
Faisal Basra

what if I am parsing the XML from URL not from file? So I can’t modify the content in URL?

sharath
Guest
sharath

Thank you so much, mk-young is mini google for me. Hats off to u sir

test
Guest
test

Hi All
Check Encoding “UTF-8 without BOM” in notepad++
if nothing is there b4

Maciej
Guest
Maciej

Thanks, worked for me! :)

anonymous
Guest
anonymous

Thanks worked for me too!!

chitra
Guest
chitra

U r really genious man, what ever im searching for, im getting from u only, thanks guru

Vishal
Guest
Vishal

I am facing the same issue. XML is below,

Vishal
Guest
Vishal
Vishal
Guest
Vishal
<?xml version="1.0"?>
<!DOCTYPE FCCFT SYSTEM "./FCCFT.DTD">
<FCCFT>
</FCCFT>
Vishal
Guest
Vishal
public static void main(String[] args) {
	String p_message = "<?xml version=\"1.0\"?>"+
			"<!DOCTYPE FCCGENERIC SYSTEM \"./FCCGENERIC.DTD\">"+
			"<FCCGENERIC>"+
				"<REPLY_ACK>"+
				    "<REQ_TYPE>UPLOAD_PMNT</REQ_TYPE>"+
				    "<XREF>406550133038787</XREF>"+
				"</REPLY_ACK>"+
			"</FCCGENERIC>";
	DOMParser domParser = null;
	Document  xmlDocument;
	Element documentElement;
	StringReader	l_reader 	= null;
	InputSource	l_in_source	= null;

	if (domParser == null) {		
	domParser	= new DOMParser ();
	}

	try {
	l_reader	= new StringReader (p_message);
	l_in_source = new InputSource (l_reader);
	domParser.parse (l_in_source);
	l_reader.close ();
	xmlDocument		= domParser.getDocument ();
	documentElement	= xmlDocument.getDocumentElement ();
} finally {
	try {
	l_reader.close ();
} catch (Exception e1) {
}
	l_reader	= null;
	l_in_source	= null;
}
vijay
Guest
vijay

awesome wasted almost half an hour for this silly typing mistake..

apoorva
Guest
apoorva
Hi Mkyong, I am facing the same error. I check my xml file to ensure that it doesn’t contain any invalid characters but I am still facing the same error. Here is my xml file content: <?xml version="1.0" ?> <survey> </survey> And following is the java code I am using import java.io.*; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SurveyParser { public static void main(String[] args) { try { InputStream xmlInput = new FileInputStream("C:/Users/kumarap/sampleData2.xml"); System.out.println(xmlInput.read()); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); SaxHandler handler = new SaxHandler(); if(xmlInput.available() > 0){ saxParser.parse(xmlInput, handler); //Let's print… Read more »
Ledostuff
Guest
Ledostuff
trackback
Java XML Tutorials

[…] SAX Error – Content is not allowed in prolog Common SAX error for invalid XML file. […]