Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 267
ericso Avatar asked Apr 08 '10 12:04

ericso


People also ask

How is XML passing done with sax?

SAX: the Simple API for XML SAX is an API used to parse XML documents. It is based on events generated while reading through the document. Callback methods receive those events. A custom handler contains those callback methods.

What is unexpected EOF in Prolog?

The evidence suggests that you have actually attempted to parse an empty stream. It says that the EOF was found while trying to parse the prolog. There is nothing wrong with the prolog in the XML you have shown us, and in particular there is no plausible reasons for the parser to encounter an EOF.


1 Answers

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]", ""); 
like image 199
Gopal Avatar answered Oct 02 '22 11:10

Gopal