[AS2] Handle Special Characters in XML

When loading XML File and parsing, usually use ChildNode[n].toString() for extract the data, sound like it can get the data and convert to “String” or other variable type. Normally, it has no problem, but when the XML contains special characte, it will get back wrong data, for example special characters like: ‘, &, ". Here is an example, let say being loaded XML File is:<?xml version="1.0" encoding="UTF-8" ?>
<data>
  <sentence><![CDATA[Luar's "Friends" & Friends]]>ActionScript:var xml:XML = new XML();
xml.ignoreWhite = true;
xml.onLoad = function() {
  var node:XMLNode = this.firstChild.childNodes[0];
  trace(node.childNodes[0]);
  trace(node.childNodes[0].toString());
  trace(node.childNodes[0].nodeValue);
};
xml.load("sample.xml");
trace result:Luar&apos;s &quot;Friends&quot; &amp; Friends
Luar&apos;s &quot;Friends&quot; &amp; Friends
Luar's "Friends" & Friends

So, the safest way to do so should be using nodeValue.

If use AS3 and E4X, no such problem: var xml:XML = new XML();
var xmlFile:String = "sample.xml";
var urlReq:URLRequest = new URLRequest(xmlFile);
var urlLoader:URLLoader = new URLLoader(urlReq);
urlLoader.addEventListener("complete", xmlLoaded);
function xmlLoaded(event:Event):void {
  xml = XML(urlLoader.data);
  trace(xml.sentence.toString());
}
trace result:Luar's "Friends" & Friends

[Download Fla]