XML technologies / XML / XML in a Nutshell / Internationalization / The default character set for XML documents
(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)
Before an XML parser can read a document, it must know the document’s character set and encoding. Sometimes external metadata tells the parser which encoding is used. For example, an HTTP header could include a Content-type header, as shown below:
Content-type: text/html; charset=ISO-8859-1However, XML parsers generally cannot rely on such information being available. Even when it is, they cannot necessarily assume it is correct. An XML parser therefore tries to infer the character set from the document’s first characters. The main tests performed by the parser include the following:
If the document’s first two bytes are #xFEFF, the parser recognizes them as a Unicode-byte-order mark. It then assumes the document uses the MSB, UTF-16 encoding of Unicode. With this information, it can read the rest of the document.
If the document’s first two bytes are #xFFFE, the parser recognizes the LSB form of the Unicode byte-order mark. It then knows that the document uses Unicode’s LSB UCS-2 encoding and can use this information to read the rest of the document.
If the first four bytes are #x3C3F786D, representing the ASCII characters
<?xm, the parser assumes the file uses an ASCII superset. Specifically, it assumes the file uses Unicode’s UTF-8 encoding. Even if this assumption is wrong, it provides enough information to read as far as the encoding declaration and determine the actual character set.
Parsers that understand EBCDIC or UCS-4 could use similar heuristics to recognize those encodings. However, UCS-4 is not yet really used and is mainly of theoretical interest, while EBCDIC is an old family of character sets that should not be used in new documents. Neither of these character sets is important in practice.
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "XML in a Nutshell" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top