• flickr
  • linkedin
  • twitter
  • rss
HTML Characters in XML

HTML Characters in XML

I’ve been doing some work with XML in AS3 and needing to have the XML contain HTML characters for an htmltext field in Flash – for links and lists etc.

Few ways to approach that:

<![CDATA[
"Rampant <'s and &'s in the XML fields that XML ignores due to special CDATA tags within the fields"
]]>

Or you could do all your html formatting in the XML with [‘s instead for example, then in Flash, change them to <‘s before firing it out. There are a million tidier ways to do this but this old function does that job:

function replaceSpecialCharracters(stringToBeReplaced:String)
{
  // pushing this string into the array one character at a time
  var stringArray:Array = new Array();
  for(var i:uint = 0;i<stringToBeReplaced.length; i++) {
    stringArray.push(stringToBeReplaced.charAt(i));
  }
  // once array filled, stripping ['s for <'s and dumping them in to replacementString
  var replacementString:String = "";
  for(i = 0;i<stringArray.length; i++) {
    if(stringArray[i] == "[") {
      stringArray[i] = "<";
    } else if (stringArray[i] == "]") {
      stringArray[i] = ">";
    } else if (stringArray[i] == "") { // damn horizontal tabs!
      stringArray[i] = "";
    }
    replacementString += stringArray[i];
  }
  return replacementString;
}

Infact this way is much better

myText = myText.split("[").join("]");
Tech Reference:

Larry's a 30-something chap interested in travel, being a dork and changing the world via less boring training.

Leave a reply