Unexpected EOF in prolog when parsing XML

java, soap, xml

Solution

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.

Problem

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?

Original source