# Invalid byte 1 of 1-byte UTF-8 sequence

Source: https://xmlvalid.com/errors/invalid-byte-1-of-1-byte-utf-8-sequence

> The input is read as UTF-8 but contains bytes that are not valid UTF-8.

The message, as Java reports it: `Invalid byte 1 of 1-byte UTF-8 sequence.`

## Why it happens

- A file saved as Windows-1252 or ISO-8859-1 without an `encoding` in the XML declaration, so the parser assumes UTF-8. Typographic quotes and dashes from Word are the usual culprits.
- XML built in Java with `String.getBytes()` or a `FileWriter` using the platform charset, then parsed as UTF-8.
- A declaration that says UTF-8 on a file that isn't.

## How to fix it

- Make the declaration match the bytes: save the file as UTF-8, or declare the encoding it really has (`<?xml version="1.0" encoding="windows-1252"?>`). In Java, write with an explicit charset (`getBytes(StandardCharsets.UTF_8)`, `Files.newBufferedWriter(path, UTF_8)`). *Invalid byte 2 of 3-byte UTF-8 sequence* is the same problem.
- 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 [Invalid bytes in character encoding](https://xmlvalid.com/errors/invalid-bytes-in-character-encoding): `Invalid bytes in character encoding`

## Example that fails

```xml
<note>it’s here</note>
```

## Fixed

```xml
<note>itâ€™s here</note>
```
