XML technologies / XML / XML in a Nutshell / Internationalization / Converting between character sets

(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)

The ultimate solution to this character-set confusion is to use Unicode for all XML documents, in either UTF-16 or UTF-8 format. More and more tools support one of these formats natively; even Notepad can save Unicode files on Windows NT 4.0, 2000, and XP. Microsoft Word 97 and later store document text in Unicode, although Word files, unlike XML, contain little plain text. Much of the binary data in a Word file is not Unicode or any other text. Word 2000 can, however, save plain text in Unicode. To do this, select Encoded Text from the Format menu in Word’s Save dialog. Then choose one of the four Unicode formats in the next dialog. In Word 2003, choose the Plain Text format. On saving, Word asks for an encoding. Choose Other Encoding, then select one of the four Unicode formats in the list on the right.

Most current tools still use vendor-specific character sets that support only a few languages at once. Learning to convert documents from proprietary to standard character sets is therefore essential.

Some of the better XML and HTML editors let you choose a character set when saving and automatically convert from the editing character set. On Unix the native character set is probably one of the standard ISO character sets, so you can save directly in that format. On a Mac avoid problems by sticking to plain ASCII documents. On Windows you can go a little further and use Latin-1, provided you avoid extra characters outside the official ISO-8859-1 specification. Otherwise, convert the document from its native platform-dependent encoding to a standard platform-independent character set.

François Pinard wrote an open-source conversion tool for character sets called recode that runs on Linux and Unix. Wojciech Galazka ported recode to DOS. You can also use the tool native2ascii from the Java Development Kit (JDK). First convert the file from its native encoding to Java’s special ASCII-encoded Unicode format. Then use the same tool in reverse to convert from Java’s format to the intended encoding. For example, to convert the file meinedatei.xml from Windows Cp1252 encoding to UTF-8, run these commands in order:

% native2ascii -encoding Cp1252 meinedatei.xml meinedatei.jtx
% native2ascii -reverse -encoding UTF-8 meinedatei.jtx meinedatei.xml

  

 

 

 

back to top