Java 解析 XML 时序言中出现意外的 EOF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40108159/
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-11 22:25:00  来源:igfitidea点击:

Unexpected EOF in prolog when parsing XML

javaxmlsoap

提问by Dimitris P.

I have this XML Document which is the body of a SOAP request:

我有这个 XML 文档,它是 SOAP 请求的主体:

<?xml version="1.0" encoding="UTF-8"?>
<mes:SubmitStructureRequest xmlns:mes="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common" xmlns:str="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/structure" xmlns:reg="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/registry" xmlns:web="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/webservices">
            <mes:Header>
               <mes:ID>TEST_DFD</mes:ID>
               <mes:Test>true</mes:Test>
               <mes:Prepared>2013-10-10</mes:Prepared>
               <mes:Sender id="TESTER"/>
               <mes:Receiver id="ESTAT"/>
            </mes:Header>
            <mes:SubmitStructureRequest action="Append">
               <str:Structures>
                  <str:Dataflows>
                     <str:Dataflow agencyID="ESTAT" id="DFD_TEST_21" version="1.0">
                        <com:Name xml:lang="en">Production in construction, total, building construction, civil engineering (Monthly)</com:Name>
                        <str:Structure>
                           <Ref agencyID="ESTAT" class="DataStructure" id="STS" package="datastructure" version="2.0"/>
                        </str:Structure>
                     </str:Dataflow>
                  </str:Dataflows>
               </str:Structures>
            </mes:SubmitStructureRequest>
</mes:SubmitStructureRequest>

I'm trying to parse it using this piece of Java code (The stream is the aforementioned xml):

我正在尝试使用这段 Java 代码来解析它(流是前面提到的 xml):

InputStream stream = sourceData.getInputStream();
        try {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            XMLStreamReader parser = factory.createXMLStreamReader(stream);
            while (parser.hasNext()) {
                int event = parser.next();
                if (event == XMLStreamConstants.START_ELEMENT) {
                    for(int i = 0 ; i < parser.getNamespaceCount(); i ++) {
                        String ns = parser.getNamespaceURI(i);
                        if(SdmxConstants.getNamespacesV1().contains(ns)) {
                            return SDMX_SCHEMA.VERSION_ONE;
                        }
                        if(SdmxConstants.getNamespacesV2().contains(ns)) {
                            return SDMX_SCHEMA.VERSION_TWO;
                        }
                        if(SdmxConstants.getNamespacesV2_1().contains(ns)) {
                            return SDMX_SCHEMA.VERSION_TWO_POINT_ONE;
                        }
                    }
                    throw new SdmxSyntaxException("Can not get Scheme Version from SDMX message.  Unable to determine structure type from Namespaces- please ensure this is a valid SDMX document");
                }
            }
            throw new SdmxSyntaxException(ExceptionCode.XML_PARSE_EXCEPTION, "No root node found");
        } catch(XMLStreamException e) {
            throw new SdmxSyntaxException(ExceptionCode.XML_PARSE_EXCEPTION, e);
        } finally {
            if(stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

At the point of int event = parser.next();I get:

在这一点上int event = parser.next();我得到:

com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog

com.ctc.wstx.exc.WstxEOFException:序言中出现意外的 EOF

Any ideas why is this happening?

任何想法为什么会发生这种情况?

回答by Stephen C

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. Hence, I infer that the XML you have shown us is not what the parser is actually seeing.

它说在尝试解析序言时发现了 EOF。您向我们展示的 XML 中的序言没有任何问题,特别是解析器没有合理的理由遇到 EOF。因此,我推断您向我们展示的 XML 并不是解析器实际看到的。

回答by Agent96

This exact error is very difficult to replicate, I know as I have spent a long long time trying to get this exact error message.

这个确切的错误很难复制,我知道我花了很长时间试图获得这个确切的错误消息。

An empty stream does not throw this exception. The way to get this specific exception is the following:

空流不会抛出此异常。获取此特定异常的方法如下:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream stream = new ByteArrayInputStream(bos.toByteArray());
        try {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            XMLStreamReader parser = factory.createXMLStreamReader(stream);
            while (parser.hasNext()) {
               int event = parser.next();
               //Exception is thrown
            }

If, like me, you try to recreate this just by constructing a ByteArrayInputStream using new byte[0] or by reading an empty file, then you will get this exception.

如果像我一样,您尝试仅通过使用 new byte[0] 构造 ByteArrayInputStream 或读取空文件来重新创建它,那么您将收到此异常。

javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Premature end of file