javaxmlhttpurlconnection

Why am I getting Premature End of File Error?


I am trying to parse an XML response, but I am failing miserably. I thought initially that the xml was just not being returned in the response, so I crafted up the code below with a direct link to my xml file online. I am able to print the XML to screen with no problems. However when I call my parse method I get Premature end of file.

It works if I pass the URL directly:

but fails when I passed an InputStream:

The errors I am getting:

    [Fatal Error] :1:1: Premature end of file.
org.xml.sax.SAXParseException: Premature end of file.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    at com.ameba.api.network.MainApp.parseDoc(MainApp.java:78)
    at com.ameba.api.network.MainApp.main(MainApp.java:41)

Solution

  • When you do this,

    while((inputLine = buff_read.readLine())!= null){
            System.out.println(inputLine);
        }
    

    You consume everything in instream, so instream is empty. Now when try to do this,

    Document doc = builder.parse(instream);
    

    The parsing will fail, because you have passed it an empty stream.