How to Specify Encoding for XML
Developers who create programs to read and process XML files face an odd paradox when it comes to determining each file's encoding, according to the W3C recommendation for XML. The difficulty arises because the encoding declaration is part of the XML file's internal information, but the program processing the file must know the correct encoding before it can read that internal declaration. To enable smooth, error-free processing of an XML file, the file's actual encoding structure must agree with the internal encoding declaration. This is simple to accomplish using a text editor like Windows Notepad.
Instructions
-
-
1
Click "Start," "All Programs," "Accessories," "Notepad" to launch Windows Notepad.
-
2
Click "File," "Open" from Notepad's menu bar to view the "Open" dialog. Navigate to the desired XML file. Click the "Open" button to view the file in Notepad.
-
-
3
Locate the first line of the file, which is the XML declaration beginning with "<?xml" and which may or may not already include an "encoding=" attribute. If the encoding is present, check to see that it refers to the correct character set. If not, add "encoding=" and the character set code inside the brackets. The result will resemble one of the following.
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-16"?>
<?xml version="1.0" encoding="ISO-8859-1"?>
-
4
Click "File," "Save As" from Notepad's menu bar to open the "Save As" dialog. Locate the "Encoding" drop-down list near the bottom of the dialog window. Select "ANSI" or "UTF-8" if the XML file's encoding is a single-byte character set, such as ISO-8859-1 or windows-1252. Select "Unicode" if the XML file's encoding is a double-byte character set like UTF-16. Click the "Save" button to save the encoded XML file.
-
1
Tips & Warnings
If the XML encoding declaration entered in Step 3 doesn't match the actual encoding of the file specified in Step 4, most XML parsers will not be able to process the file correctly.