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.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- Parse errorsStart tag expectedThe message a stray BOM in the wrong place produces.
- Parse errorsXML declaration allowed only at the start of the documentThe related failure when anything else gets in front of the declaration.
Related terms
- Encoding declarationThe encoding pseudo-attribute in the XML declaration, telling the parser which character encoding the file's bytes are in.
- UTF-8The default encoding for XML, and the one every conforming parser must support along with UTF-16.
- PrologEverything in a document before the root element's start tag: the XML declaration, any comments or processing instructions, and the document type declaration.
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.