# Premature end of file

Source: https://xmlvalid.com/errors/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

libxml2, which xmlvalid validates with, reports it as [Document is empty](https://xmlvalid.com/errors/document-is-empty): `Document is empty`

## Example that fails

```xml

```

## Fixed

```xml
<note/>
```
