Java 如何解决[org.xml.sax.SAXParseException; 行号:1;列数:1;序言中不能有内容。]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42576321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:15:04  来源:igfitidea点击:

how to solve [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]

javaxml

提问by Sharon Wong

the below is example my xml enter link description here

以下是我的 xml在此处输入链接描述的示例

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.]"

运行“nmo”时,出现错误“javax.xml.bind.UnmarshalException - 链接异常:[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

谢谢和最好的问候莎伦

回答by Matthias Danetzky

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 文件在序言之前写入了一些数据。字符串之前应该没有任何内容,看起来像这样:

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

回答by Michael Kay

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.

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

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.

返回可用于读取内容的 Reader,无需将该内容提交给 XML 解析。