Home > flex > AS3 htmlDecode htmlEncode xml hack

AS3 htmlDecode htmlEncode xml hack

You’d think there would be a straight forward way to htmlEncode and htmlDecode strings in flash but there doesn’t seam to be any sort of built-in utility class that supports this. Oddly enough there isn’t one in Javascript either. Like this Javascript hack, here is a handy hack to pimp AS3’s native XML object instance to do the htmlEncoding/decoding for you.

Unfortunately this doesn’t work for  many encoded characters like “í”, “ó”,  “á”, and “ñ”. It seams the best route to robust html html encoding/decoding is to roll your own utility function like this.

Categories: flex Tags:
  1. Aran
    February 25th, 2009 at 17:26 | #1

    You could use this class:
    http://dynamicflash.com/goodies/base64/

    A comment from someone else using the class which seems to be similar to your use-case:

    “Oh I also wanted to tell you how much I was able to trim by combining compress and Base64 encode. Note: I am working on an LMS called CourseMill that URLEncodes everything, turned out to be in my favor.

    Originally, I was just passing an XMLString, which was roughly 65000 characters long after it was URLEncoded. Then I tried compress(), which reduced it to a very small amount of characters, probably under 1000. But when I stored the compress var onto the LMS it was URLEncoded and shot up to around 33000 characters. The shining star here is the Base64 encoding which is 99.9% safe from URL Encoding. So after compressing AND encoding with Base64, my var was reduced to a 13000!!!

    VERY COOL!!!”

  2. February 26th, 2009 at 11:35 | #2

    Thats cool Aran, unfortunately it doesn’t work with my use case where I need to explicitly decode html encoded characters that have been sent to me via external RSS feeds.

  3. Arie
    February 12th, 2010 at 04:29 | #3

    Thanks,

    Converted the mentioned js method into AS and placed the code in a Util class.
    Works very well.

  1. No trackbacks yet.