An invalid XML character (Unicode: 0x…) 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 , 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

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

PCDATA invalid Char value 1

Example

Reports the error
<note>a\x01b</note>
Fixed
<note>a b</note>