Premature end of file

The parser reached the end of the input before finding a root element.

The message, as Java reports it
Premature end of file.

Why it happens

  • An empty file or response: zero bytes, or only whitespace and an XML declaration.
  • An InputStream that was already read: logged, parsed once, or consumed by a framework before your code gets it.
  • An HTTP call that returned no body, such as a 204 or a redirect that was not followed.

How to fix it

Check the length of what you parse. A stream can be read only once: read it into a byte array if you need it twice, or reset a BufferedInputStream with mark() and reset().

Java's schema validation (javax.xml.validation.Validator, and JAXB or Spring on top of it) throws at the first error unless you set an ErrorHandler whose error() records the SAXParseException and carries on; each one has getLineNumber() and getColumnNumber(). A well-formedness error ends parsing whatever the handler does.

The same problem in xmlvalid

xmlvalid validates with libxml2, which reports it as Document is empty. It checks the whole document in your browser and lists every schema error with its line and column, not only the first.

Document is empty

Example

Reports the error
(an empty file)
Fixed
<note/>