Skip to content

XML Encoding

XML documents are text-based and can contain characters from various languages worldwide (e.g., English, French éàç, Japanese かな, Russian Язык).

To ensure these characters are correctly stored, transmitted, and interpreted, specifying the character encoding is crucial.

A character encoding (or character set, charset) defines a mapping between characters (like ‘A’, ’€’, ‘猫’) and the sequences of bytes used to represent them in computer memory and files.

Without knowing the correct encoding, software might misinterpret the bytes, leading to garbled text (mojibake) or errors.

The Unicode Standard is the universal character encoding standard. It aims to assign a unique number (code point) to every character used in modern computing.

UTF-8 (Unicode Transformation Format - 8-bit) is the most common way to implement Unicode for storage and transmission, especially on the web.

Key features of UTF-8:

  • Variable-width: Uses 1 byte for basic Latin characters (ASCII compatible), and up to 4 bytes for other characters.
  • Universal: Can represent any character in the Unicode standard.
  • Widely Supported: It’s the default encoding for HTML5, JSON, and recommended for XML, JavaScript, Python 3, and many other technologies.
  • Efficient for predominantly English text while still supporting all languages.

Other Unicode encodings like UTF-16 exist but are less common for web and general file exchange.

The recommended way to specify the encoding of an XML document is in the XML declaration (the optional first line):

<?xml version="1.0" encoding="UTF-8"?>

This explicitly tells the XML parser how to interpret the bytes in the file.

XML Standard Requirements:

  • All XML parsers must support UTF-8 and UTF-16.
  • If the encoding attribute is omitted, the parser will typically assume UTF-8 (or attempt auto-detection based on byte order marks, BOMs, but relying on the declaration is safer).
  • While parsers may support other encodings (like ISO-8859-1, Windows-1252), using UTF-8 maximizes compatibility.

Encoding issues often arise when:

  • An XML file is saved with one encoding (e.g., Windows-1252) but declared as another (e.g., UTF-8).
  • The encoding declaration is missing, and the actual encoding is not UTF-8.
  • Software processing the XML (editor, server, browser, parser) doesn’t respect or correctly handle the specified encoding.
  • Data is transferred between systems with different default encodings.

Resulting problems include incorrect character display (e.g., ‘ü’ showing as ‘ü’) or fatal parsing errors.

To avoid encoding problems with XML:

  • Use UTF-8 whenever possible. It’s the de facto standard for the web and handles all characters.
  • Always declare the encoding in the XML declaration: <?xml version="1.0" encoding="UTF-8"?>.
  • Ensure your editor saves the file using the declared encoding. Most modern editors support saving as UTF-8.
  • Verify that systems transmitting or receiving XML handle encodings correctly. Configure servers and applications appropriately.