Java parsing XML document gives "Content not allowed in prolog." error


I am writing a program in Java that takes a custom XML file and parses it. I'm using the XML file for storage. I am getting the following error in Eclipse.

[Fatal Error] :1:1: Content is not allowed in prolog.
org.xml.sax.SAXParseException: Content is not allowed in prolog.
    at  )
    at javax.xml.parsers.DocumentBuilder.parse(
    at me.ericso.psusoc.RequirementSatisfier.parseXML(
    at me.ericso.psusoc.RequirementSatisfier.getCourses(
    at     me.ericso.psusoc.programs.RequirementSatisfierProgram.main(  )

The beginning of the XML file is included:

<?xml version="1.0" ?>
     <Major id="IST">
        <name>Information Science and Technology</name>
        <option> Information Systems: Design and Development Option</option>
            <firstlevel type="General_Education" credits="45">
                <component type="Writing_Speaking">GWS</component>
                <component type="Quantification">GQ</component>

The program is able to read in the XML file but when I call DocumentBuilder.parse(XMLFile) to get a parsed org.w3c.dom.Document, I get the error above.

It doesn't seem to me that I have invalid content in the prolog of my XML file. I can't figure out what is wrong. Please help. Thanks.

4/8/2010 1:23:34 PM

I think this is also a solution of this problem.

Change your document type from 'Encode in UTF-8' To 'Encode in UTF-8 without BOM'

I got resolved my problem by doing same changes.


Make sure there's no hidden whitespace at the start of your XML file. Also maybe include encoding="UTF-8" (or 16? No clue) in the node.


The document looks fine to me but I suspect that it contains invisible characters. Open it in a hex editor to check that there really isn't anything before the very first "<". Make sure the spaces in the XML header are spaces. Maybe delete the space before "?>". Check which line breaks are used.

Make sure the document is proper UTF-8. Some windows editors save the document as UTF-16 (i.e. every second byte is 0).


I assume you have proper xml encoding and matching with Schema.

If you still get this error, check code that unmarshalls the xml and input type you have used. Because XML documents declare their own encoding, it is preferable to create a StreamSource object from an InputStream instead of from a Reader, so that XML processor can correctly handle the declared encoding [Ref Book: Java in A Nutshell ]

Hope this helps!


You are not providing the correct address for the file. You need to provide an address such as C:/Users/xyz/Desktop/myfile.xml


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]