# An invalid XML character (Unicode: 0x…) was found

Source: https://xmlvalid.com/errors/invalid-xml-character-was-found

> The document contains a control character that XML 1.0 does not allow, even escaped.

The message, as Java reports it: `An invalid XML character (Unicode: 0x1) was found in the element content of the document.`

## Why it happens

- Control characters copied from a database, a log or a binary field: 0x0 to 0x1F other than tab, line feed and carriage return.
- A character reference to one of them, such as `&#1;`, which is not allowed either.

## How to fix it

- Remove the character, or replace it before writing the XML; binary data belongs in Base64. Filtering out code points the XML 1.0 `Char` production does not allow is a common step when exporting from a database.
- 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 [PCDATA invalid Char value](https://xmlvalid.com/errors/pcdata-invalid-char-value): `PCDATA invalid Char value 1`

## Example that fails

```xml
<note>ab</note>
```

## Fixed

```xml
<note>a b</note>
```
