Skip to main content
XMLDir

Search 309 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

Encoding and characters

Byte order mark

The optional U+FEFF character at the start of a file, marking the encoding and — in UTF-16 — its byte order.

In UTF-16 a BOM is required, because there is no other way to tell the two byte orders apart. In UTF-8 it is permitted, carries no ordering information, and is mostly a Windows habit. XML tolerates it: a BOM may precede the XML declaration without breaking the rule that nothing comes before it.

The trouble starts downstream. A BOM is invisible in an editor, survives concatenation, and appears in the middle of the result — where it is no longer a BOM but a stray character in content, and quite possibly the reason a second document will not parse.

Defined in
XML 1.0 Appendix F
Category
Encoding and characters
Also written
BOM, U+FEFF, EF BB BF

Shown, not asserted

All 2 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.

  • A UTF-8 BOM before the declaration is legal and does not displace it.

    document.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <x>hi</x>

    What the engine returns

    Well-formed.
  • The same character anywhere else is content, and content cannot precede the declaration.

    document.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <x>hi</x>

    What the engine returns

    Start tag expected, '<' not found (line 2)

What catches people out

  • A UTF-8 BOM is legal in XML and illegal in plenty of things that consume XML. Prefer writing files without one.
  • Concatenating files that each begin with a BOM buries one in the middle, where it is an ordinary — and usually fatal — character.
  • Editors hide it. If a file will not parse and the first character looks fine, check the bytes.

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.