Unexpected EOF in prolog when parsing XML

42,136

Solution 1

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.

Solution 2

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

Solution 3

This error comes due to various reasons, and it is very hard to pinpoint one, basically the most common factor for this error is that the xml request that you are sending or the xml response that is supposed to come back to you is not in xml format, due to which it is not able to parse, try executing the request in a soap ui, it will help to narrow down the error in a better way.

Share:
42,136
Dimitris P.
Author by

Dimitris P.

Updated on March 16, 2021

Comments

  • Dimitris P.
    Dimitris P. about 3 years

    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?