Introduction
In the years that I've been working with Flash, I've seen a lot of people - including myself - having trouble with showing XML in Flash correctly. With correctly I mean all characters are shown as they are supposed to be shown. This has everything to do with how the data is represented in the XML file and under what encoding that file is saved.
How the data should be represented
Some characters will cause an error when you try to preview your XML file. To solve this problem you need to replace them by entity references. XML has 5 predefined entity references.
| entity | character | meaning |
| < | < | less than |
| > | > | greater than |
| & | & | ampersand |
| ' | ' | apostrophe |
| " | " | quotation mark |
When you have a lot of the characters in a string, you can use a CDATA node. Everything inside a CDATA section will be ignored by the parser. A practical example of this is ActionScript in Flex MXML files.
-
<![CDATA[
-
function matchwo(a,b){
-
if (a <b && a <0) {
-
return 1;
-
}else{
-
return 0;
-
}
-
}
-
]]>
How to save your XML files
The standard encoding Flash uses is UTF-8. You might think that putting the codepage in the XML header will make your data Flash compatible but unfortunately that doesn't do the job.
-
<?xml version="1.0" encoding="utf-8"?>
In fact, since utf-8 is the standard codepage for Flash, it is not even needed to specify the encoding in the header. Instead, you have to save your file under utf-8 encoding. To do this, open your XML file in NotePad, choose "Save As..." from the file menu and select "UTF-8" in the encoding combobox.

When you save your file this way, NotePad writes a BOM (Byte Order Mark) in your XML file which makes it possible for Flash to understand the file's encoding.
What is a BOM?
A byte order mark (BOM) consists of the character code U+FEFF at the beginning of a data stream, where it can be used as a signature defining the byte order and encoding form, primarily of unmarked plaintext files. Under some higher level protocols, use of a BOM may be mandatory (or prohibited) in the Unicode data stream defined in that protocol.
Conclusion
When you follow these simple guidelines, you should have no problems loading client-side XML data into Flash. In the next article (this is 1 of 2) I will cover loading server-side XML data. Server-side XML data as in generated from a middleware page like a ASP or PHP page. You'll see that both ASP and PHP offer some nice functionality to do this.
Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
Christophe Herreman is a software developer living in Belgium. He's working on high-end Flex and AIR solutions at 
Recent Comments