org.xml.sax.SAXParseException: Premature end of file for *VALID* XML org.xml.sax.SAXParseException: Premature end of file for *VALID* XML xml xml

org.xml.sax.SAXParseException: Premature end of file for *VALID* XML


It is a problem with Java InputStream. When the stream is read once the file offset position counter is moved to the end of file. On the subsequent read by using the same stream you'll get this error. So you have to close and reopen the stream again or call inputStream.reset() to reset the offset counter to its initial position.


This is resolved. The problem was elsewhere. Another code in cron job was truncating XML to 0 length file. I have taken care of that.


This exception only happens if you are parsing an empty String/empty byte array.

below is a snippet on how to reproduce it:

String xml = ""; // <-- deliberately an empty string.ByteArrayInputStream xmlStream = new java.io.ByteArrayInputStream(xml.getBytes());Unmarshaller u = JAXBContext.newInstance(...)u.setSchema(...);u.unmarshal( xmlStream ); // <-- here it will fail