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“.

Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.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

  • Pingback: read more()

  • Pingback: this post()

  • Pingback: alkaline water()

  • Pingback: alkaline water()

  • Pingback: visit site()

  • Pingback: water ionizer loans()

  • Pingback: you can try these out()

  • Pingback: house blue()

  • Pingback: zak?ad szklarski piotrk√≥w trybunalski()

  • Pingback: locksmiths in zachary la()

  • Pingback: locksmith jobs in florida()

  • Pingback: local 3 electricians()

  • Pingback: electrician job outlook()

  • Pingback: alkaline water brands()

  • Pingback: water ionizer pay plan loans()

  • Pingback: pay per day loans plan()

  • Pingback: pay per day loan plans()

  • Pingback: Instagram volgers kopen()

  • Pingback: alkaline water()

  • Pingback: sms laan nu()

  • Pingback: stop parking()

  • Pingback: car parking()

  • Pingback: lan penge nu uden renter()

  • Pingback: 3gp mobile porn()

  • Pingback: best DIRECTV deals()

  • Pingback: Tanner deep throat()

  • Pingback: watch movies online free()

  • Pingback: watch tv show episodes()

  • Pingback: Blue Coaster33()

  • http://www.javaplex.com Faisal Basra

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

  • sharath

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

  • http://test.com test

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

  • chitra

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

  • Vishal

    I am facing the same issue. XML is below,

    • Vishal
    • Vishal
      &lt;?xml version=&quot;1.0&quot;?&gt;
      &lt;!DOCTYPE FCCFT SYSTEM &quot;./FCCFT.DTD&quot;&gt;
      &lt;FCCFT&gt;
      &lt;/FCCFT&gt;
      • Vishal
        public static void main(String[] args) {
        	String p_message = &quot;&lt;?xml version=\&quot;1.0\&quot;?&gt;&quot;+
        			&quot;&lt;!DOCTYPE FCCGENERIC SYSTEM \&quot;./FCCGENERIC.DTD\&quot;&gt;&quot;+
        			&quot;&lt;FCCGENERIC&gt;&quot;+
        				&quot;&lt;REPLY_ACK&gt;&quot;+
        				    &quot;&lt;REQ_TYPE&gt;UPLOAD_PMNT&lt;/REQ_TYPE&gt;&quot;+
        				    &quot;&lt;XREF&gt;406550133038787&lt;/XREF&gt;&quot;+
        				&quot;&lt;/REPLY_ACK&gt;&quot;+
        			&quot;&lt;/FCCGENERIC&gt;&quot;;
        	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

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

  • 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:

    &lt;?xml version=&quot;1.0&quot; ?&gt;
    &lt;survey&gt;
    &lt;/survey&gt;

    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(&quot;C:/Users/kumarap/sampleData2.xml&quot;);
    			    System.out.println(xmlInput.read());
     
     
    			    SAXParserFactory factory = SAXParserFactory.newInstance();
    	            SAXParser saxParser = factory.newSAXParser();
    	            SaxHandler handler   = new SaxHandler();
    	            if(xmlInput.available() &gt; 0){
    	            	saxParser.parse(xmlInput, handler);
     
    	            	//Let's print the values
    	            	System.out.println(handler.getSurveyMeta().toString());
    	            }	
     
    		   }catch (Exception e) {
    		       e.printStackTrace();
    		     }		
     
     
    	}
     
    }

    And the stack trace of exception is:

    org.xml.sax.SAXParseException: Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1411)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1017)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:645)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:198)
    at com.ec.barometer.porting.SurveyParser.main(SurveyParser.java:25)

  • Pingback: Java XML Tutorials()