UTF-16 Byte Order Mark (BOM) in XML Explained

The Byte Order Mark (BOM) is a critical sequence of bytes placed at the very beginning of a UTF-16 encoded XML file to indicate both the character encoding and the byte ordering (endianness) used to store the data. In XML processing, the BOM acts as a foundational signature that allows parsers to correctly interpret multi-byte characters before parsing the document’s textual contents or reading its XML declaration. Without the BOM, XML processors may fail to decode the file, leading to parsing errors, data corruption, or misinterpretation of the underlying markup.

Understanding Endianness in UTF-16

UTF-16 encodes Unicode characters using one or two 16-bit (2-byte) code units. Because computer architectures store multi-byte values in different orders, these 16-bit units must be ordered in one of two ways:

The Byte Order Mark uses the Unicode character U+FEFF (Zero Width No-Break Space) placed at the start of the stream to explicitly signal which architecture is used:

Role in the W3C XML Specification

According to the W3C XML specification, XML processors are required to support both UTF-8 and UTF-16 encodings natively. An XML document can declare its encoding via the XML declaration (e.g., <?xml version="1.0" encoding="UTF-16"?>), but a parser cannot reliably read this declaration without first knowing the byte order of the text.

The BOM solves this “bootstrap” problem. When an XML parser encounters an incoming byte stream, it reads the initial bytes (the magic numbers) to determine the encoding:

  1. If it encounters 0xFE 0xFF, it configures its decoder for UTF-16BE.
  2. If it encounters 0xFF 0xFE, it configures its decoder for UTF-16LE.
  3. Once the byte stream can be decoded into characters, the parser safely reads the XML declaration and the rest of the document.

While the BOM is optional in UTF-8, the XML specification explicitly requires or strongly expects a BOM for any entity encoded in UTF-16 that lacks an external encoding declaration (such as an HTTP Content-Type header specifying UTF-16BE or UTF-16LE).

Consequences of a Missing or Incorrect BOM

Omitting the BOM from a UTF-16 XML file can lead to several integration issues: