Compressing XML for Flash

I've been giving some thought to compressing XML for use in Flash today and I wanted to share my results. Earlier I posted a piece on HTTP Compression and I believe that is the best solution for the problem that most people are trying to solve - namely shortening download time. There's another problem though that I want to address here, and that issue is reducing the actual storage space required.

Essentially what we want to do is to store the XML documents in a compressed format that Flash can read. Easy, you say - use Mario Klingemann's xml2swf. Essentially it wraps the XML as a String variable inside a SWF file. The SWF is readable by other pieces of flash, and it's compressed because Flash MX and upwards can use compression. So problem solved!

Not quite.

Flash strings have a 64K limit. That's a lot for most strings - but it's peanuts when you're dealing with multi-megabyte XML documents. And those are the kind of documents that I typically most want to compress. So I played around with this today and found that the 64K limit applies only to embedded strings. If the strings are dynamically created, the limit doesn't apply.

So, in order to store arbitrarily long XML documents, you firstly create a flash file to hold the XML. Call it data.swf. Then break up the XML into chunks of 63K each. Set each chunk as a different variable. Then use actionscript to amalgamate those chunks into a single string or XML object. Publish. You can then load that compressed data into other flash files and access the final string.

I put together a quick proof of concept and it works nicely - although I think i'd want to automate the process if I were using it in a project.

Posted by Alexander at February 14, 2005 04:17 PM

Make Flash tests and assessments with the Question Writer, Quiz Software. Question Writer 4 is now available. Click here to download.

Trackback Pings

TrackBack URL for this entry:


breaking strings into pieces sounds like an idea! i have to see if i get my LZ77 compressor working with this trick!

Posted by: sascha at February 14, 2005 06:22 PM

What do you think would happen if you created a dynamic text box and then set it's text to all of your massive long xml string? Did you experiment with that??

Posted by: Berris at February 18, 2005 11:04 AM

I think you would be creating a string statically to do that anyway and if it could be assigned to a text box, it would break the player, because it would try to render the whole string. :( That's my guess.

Posted by: Alexander McCabe at February 18, 2005 11:15 AM