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

JavaXmlEclipseXml Parsing

Java Problem Overview


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 com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:239)
	at     com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283  )
	at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
	at me.ericso.psusoc.RequirementSatisfier.parseXML(RequirementSatisfier.java:61)
	at me.ericso.psusoc.RequirementSatisfier.getCourses(RequirementSatisfier.java:35)
	at     me.ericso.psusoc.programs.RequirementSatisfierProgram.main(RequirementSatisfierProgram.java:23  )

The beginning of the XML file is included:

<?xml version="1.0" ?>
<PSU>
     <Major id="IST">
	    <name>Information Science and Technology</name>
		<degree>B.S.</degree>
		<option> Information Systems: Design and Development Option</option>
		<requirements>
			<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.

Java Solutions


Solution 1 - Java

Please check the xml file whether it has any junk character like this �.If exists,please use the following syntax to remove that.

String XString = writer.toString();
XString = XString.replaceAll("[^\\x20-\\x7e]", "");

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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

Solution 5 - Java

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

Solution 6 - Java

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!

Solution 7 - Java

If you're able to control the xml file, try adding a bit more information to the beginning of the file:

<?xml version="1.0" encoding="UTF-16" standalone="no"?>

Solution 8 - Java

Check any syntax problem in the XMl file. I've found this error when working on xsl/xsp with Cocoon and I define a variable using a non-existing node or something like that. Check the whole XML.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionericsoView Question on Stackoverflow
Solution 1 - JavaGopalView Answer on Stackoverflow
Solution 2 - JavaJava_AlertView Answer on Stackoverflow
Solution 3 - JavaBen JView Answer on Stackoverflow
Solution 4 - JavaAaron DigullaView Answer on Stackoverflow
Solution 5 - JavaAmit AgarwalView Answer on Stackoverflow
Solution 6 - Javaspark07View Answer on Stackoverflow
Solution 7 - JavaDrew JohnsonView Answer on Stackoverflow
Solution 8 - JavaAlfabravoView Answer on Stackoverflow