Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected EOF in prolog when parsing XML

Tags:

java

soap

xml

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

<?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):

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:

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

Any ideas why is this happening?

like image 861
Dimitris P. Avatar asked Oct 18 '16 12:10

Dimitris P.


2 Answers

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.

like image 183
Stephen C Avatar answered Sep 19 '22 11:09

Stephen C


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.

javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Premature end of file
like image 30
Agent96 Avatar answered Sep 20 '22 11:09

Agent96