Why am I getting this error Premature end of file?

For those who reached this post for Answer:

This happens mainly because the InputStream the DOM parser is consuming is empty

So in what I ran across, there might be two situations:

  1. The InputStream you passed into the parser has been used and thus emptied.
  2. The File or whatever you created the InputStream from may be an empty file or string or whatever. The emptiness might be the reason caused the problem. So you need to check your source of the InputStream.

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.


You are getting the error because the SAXBuilder is not intelligent enough to deal with "blank states". So it looks for at least an <xml ..> declaration, and when that causes a no data response it creates the exception you see rather than report the empty state.