# Content is not allowed in prolog

Source: https://xmlvalid.com/errors/content-is-not-allowed-in-prolog

> Something other than markup comes before the root element.

The message, as Java reports it: `Content is not allowed in prolog.`

## Why it happens

- The input is not XML: an HTML error page, JSON, or an empty response with a message from a server or proxy.
- Bytes before `<?xml`: a UTF-8 byte order mark read through a `Reader` that kept it, or a UTF-16 file read as UTF-8.
- Text before the root element, or a string passed where Java expects a file name or URI (or the other way round).

## How to fix it

- Look at the first bytes Java actually receives, not the file you think it reads: log them in hex. Pass the parser an `InputStream` so it detects the encoding and byte order mark itself, and check the HTTP status and `Content-Type` before parsing a response.
- 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 [Start tag expected, '<' not found](https://xmlvalid.com/errors/start-tag-expected): `Start tag expected, '<' not found`

## Example that fails

```xml
hello <note/>
```

## Fixed

```xml
<note>hello</note>
```
