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:
- Big-Endian (UTF-16BE): The most significant byte is stored first.
- Little-Endian (UTF-16LE): The least significant byte is stored first.
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:
- A byte sequence of
0xFE 0xFFindicates UTF-16BE. - A byte sequence of
0xFF 0xFEindicates UTF-16LE.
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:
- If it encounters
0xFE 0xFF, it configures its decoder for UTF-16BE. - If it encounters
0xFF 0xFE, it configures its decoder for UTF-16LE. - 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:
- Fatal Parsing Errors: If an XML parser expects
UTF-8 by default and encounters UTF-16 data without a BOM, it will
typically read alternating null bytes (
0x00) and fail immediately with a fatal “not well-formed” or “invalid token” error. - Byte Order Inversion: If a UTF-16LE stream is
parsed as UTF-16BE (or vice versa), the character
U+FEFFbecomesU+FFFE, which is a non-character in Unicode. The parser will immediately recognize this as invalid data and terminate processing. - Mojibake / Character Corruption: If an application attempts to process raw bytes without respecting the BOM, the document’s characters will be corrupted, rendering the XML unreadable.