如何解决[org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1;序言中不能有内容。]

时间:2017-03-03 10:23:36

标签: java xml

以下是我的xml示例 enter link description here

我的编码是

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

运行" nmo"时出现错误" javax.xml.bind.UnmarshalException   - 链接异常: [org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1; 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

我的编码有问题吗?如何解决我的问题?

谢谢和最诚挚的问候 沙龙

3 个答案:

答案 0 :(得分:0)

看来,你的xml文件在prolog之前写了一些数据。 在字符串之前应该没有任何内容,看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>

答案 1 :(得分:0)

错误消息“Prolog中不允许内容”可能由于各种原因而出现。它基本上意味着解析器在成功读取文档中的第一个有意义的内容之前发现了一些错误。这可能是(如消息所示),因为文档以“&lt;”之外的其他内容开头,但是当内容不可读或编码错误时也会发生。

我首先检查

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

返回一个可用于阅读内容的Reader,而不会将该内容提交给XML解析。

答案 2 :(得分:-1)

尝试这个干净的项目并重建