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:
- The
InputStream
you passed into the parser has been used and thus emptied. - The
File
or whatever you created theInputStream
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 theInputStream
.
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.