3

the below is example my xml enter link description here

my coding is

JAXBContext jaxbContext = JAXBContext.newInstance(NewsMLObj.class);
        SAXParserFactory spf = SAXParserFactory.newInstance();
        XMLReader xr = spf.newSAXParser().getXMLReader();

        // to bypass XML DocType and Entity as Jap did not provide proper XML
        xr.setFeature("http://xml.org/sax/features/validation", false);
        xr.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        xr.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        xr.setFeature("http://xml.org/sax/features/external-general-entities", false);
        xr.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        xr.setFeature("http://xml.org/sax/features/use-entity-resolver2", false);

        InputSource is = new InputSource(new FileReader(factoryType.serverXML.getInputFile2() + filename));
        SAXSource source = new SAXSource(xr, is);
        out.println("input source=" + is);
        javax.xml.bind.Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        out.println("jaxbUnmarshaller =" + jaxbUnmarshaller);
        NewsMLObj nmo = (NewsMLObj) jaxbUnmarshaller.unmarshal(source);

when running "nmo", it have error "javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]"

javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at com.n2n.NekkeiFlashNews.client.imp.PacketToObjectNewsHostServer.processRawNews(PacketToObjectNewsHostServer.java:83)
at com.n2n.NekkeiFlashNews.client.imp.NewsRawFileReceiverThread.run(NewsRawFileReceiverThread.java:57)
at java.lang.Thread.run(Unknown Source)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1;   Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
... 7 more

is it my coding have issue? how to solve my issue?

Thanks and best regards Sharon

Sharon Wong
  • 109
  • 1
  • 2
  • 14
  • Looks like your XML is not valid. Look with an hex Editor if there are non printable characters at the beginning – Jens Mar 03 '17 at 10:27
  • @Jens How to say the XML not valid? – Sharon Wong Mar 03 '17 at 10:28
  • Please kindly advice? how should i do? – Sharon Wong Mar 03 '17 at 10:30
  • I had the same issues. Turns out the IDE was generating files with random codes. go to **Build**>**Rebuild Project** it will tell you what file contains an error. Mine was the color.xml if you have the same issue, Here's [a link](https://stackoverflow.com/a/54509482/7626567) that might help. It worked for me – LaureLazard Feb 10 '20 at 09:08

2 Answers2

6

It seems, that your xml file has some data written before the prolog. There should be nothing before the string, that looks like this one:

<?xml version="1.0" encoding="UTF-8"?>
  • i just check it before – Sharon Wong Mar 03 '17 at 10:41
  • 1
    In this case you could try to filter out things that don't belong there. The simple approach would be to read the string first, clean it up and push it to the SAXParser. If the file is too big - then you could write your own filtered stream. http://www.math.uni-hamburg.de/doc/java/tutorial/essential/io/filtered.html – Matthias Danetzky Mar 03 '17 at 11:35
3

The error message "Content is not allowed in Prolog" can arise for a great variety of reasons. It basically means that the parser found something wrong before it successfully read the first meaningful content in the document. This might be (as the message suggests) because the document starts with something other than "<", but it can also happen when the content is unreadable or badly encoded.

I would start by checking that

new FileReader(factoryType.serverXML.getInputFile2() + filename)

returns a Reader that is usable for reading content, without submitting that content to XML parsing.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164