I’m never one to back down from wrestling with the most bizarre problems—let’s see who comes out on top. It’s been ages since I hit an obstacle while coding, yet today I ran into a little snag—XML generated by PHP came out as garbled text when fetched with FLASH. After some digging I finally solved it. Recording it here, and also jotting down the general solution. If you also run into XML <-> FLASH garbled text, here’s a quick reference:

1. When you’re certain the XML is absolutely fine:

First, FLASH reading XML and getting garbled text involves the static property System.useCodepage. Official description: “A Boolean value that tells Flash Player which code page to use to interpret external text files.” Defaults to false.

If we’re using UTF-8 encoded external text files, we don’t need to worry about it. But if we use a non-UTF-8 encoded text file with Chinese characters in it, we need to set it to true for FLASH to read the characters without garbling. In AS3 you can first import flash.system.System; then set System.useCodePage=true;

2. Wanting PHP to generate UTF-8 encoded XML:

To have Chinese characters in the XML, it’s best to use UTF-8 encoding. Before creating the XML with DOM, declare it as $dom\_XML = new DomDocument('1.0','UTF-8');—the second parameter corresponds to the encoding value in the XML document’s declaration section. But note: this is only the declared encoding. When you finally use $dom\_XML->saveXML();, the actual generated XML file has the same encoding as the PHP script source file, i.e. “whatever encoding your PHP is, that’s the encoding of the generated file.” (see http://www.phpchina.com/bbs/viewthread.php?tid=11450)

3. The XML file declares UTF-8 encoding, but in FLASH it’s still garbled no matter what value useCodepage is set to

This situation comes up often, usually because: although the declaration part is UTF-8, the file itself isn’t UTF-8 (mine was ANSI encoded, ugh). That leads to a serious problem: Firefox can parse the XML fine, but IE (including IE-based browsers like TT) reports invalid characters, and Flash shows garbled text too! There are two ways to handle this:

  1. Change the XML declaration part to a non-UTF-8 encoding, such as GB2312, then set FLASH’s useCodepage=true;
  2. Open the XML in Notepad, use “Save As” to save it as UTF-8.

In other words, make the encoding declaration match the file’s actual encoding—no false advertising.

4. The PHP source file isn’t UTF-8 encoded—how do you generate XML that lets FLASH support Chinese characters? This is the case I ran into:

The PHP is saved as ANSI, so the XML generated with DOM is naturally ANSI too. If an XML file in this encoding contains Chinese characters, it can’t be read correctly even if the declared encoding is UTF-8. Given this, for FLASH to read the XML correctly without garbled text, you must set encoding to GB2312. Yet PHP’s DOM apparently can’t write Chinese characters with GB2312 (no idea why—gurus, please enlighten me). If you do new DomDocument('1.0','GB2312');, saving the XML throws an error like: “output conversion failed due to conv error, bytes 0xCE 0xD2 0x5D 0x5D”. That is to say, my ANSI PHP can only generate an XML file that declares UTF-8 but is actually ANSI—and of course FLASH reads that as garbled text (see item 3). My solution: after PHP generated the XML with the UTF-8 declaration, I added one extra step: open the XML file, rewrite the header declaration, and replace UTF-8 with GB2312. Hehe—now FLASH, IE, and FF all work without errors!