divillysausages.com

XML Compressor/Uncompressor tool

The ability to compress and uncompress XML in Flash can come in useful if you're working on a project or game that's heavily data driven. It's ridiculously easy to do and the size benefits that you get make it a no-brainer. The level of compression is obviously dependant on the complexity of the XML, but as an example, I compressed my iTunes library XML file: original size: 2.91MB; compressed size: 174KB. Yeah, that could come in handy :D

The tool

As I've used this more and more in projects, I found myself getting annoyed at having to constantly write the logic to compress/uncompress XML files and save the results (I'd have to change the logic for what type of XML to expect, then write a click event to save out the file etc.), so I created the (duh duh duhhh) XML Compressor/Uncompressor!

The XML Compressor/Uncompressor tool

Simply copy in the current folder location into the Current folder TextField (necessary as FileReference in Flash doesn't give you information on the folder where a file is located, just it's name), hit Open file and select the XML you're looking for. When it's loaded in, depending on if it's compressed or uncompressed, the Compress button will change accordingly. When you're finished, just hit Save to save out your file.

I've found this quite handy for checking the layout of a compressed XML file, or to uncompress, quickly make changes, then recompress. All this for a mere 13KB!

So what's the meat of the program?

Compressing XML

The code behind compression is pretty straightforward. We simply take our XML, write it to a ByteArray and use the ByteArray.compress() method to actually compress it:

// myXML is our XML object
var ba:ByteArray = new ByteArray;
ba.writeUTFBytes( myXML );
ba.compress();

If you want to save it back out, then use the FileReference class:

var file:FileReference = new FileReference;
file.save( ba, "myXML.xml" );

Uncompressing XML

To uncompress the XML, we use the ByteArray.uncompress() method and reconstruct our XML object from it. One word of warning though: ByteArray.uncompress() will throw an Error if it's used on data that's not compressed, so unless you explicity know that your ByteArray is compressed, put it in a try..catch.

try
{
	ba.uncompress();
}
catch ( e:Error )
{
	trace( "The ByteArray wasn't compressed!" );
}

// set our xml data
myXML = XML( ba );

Loading compressed XML files

When your XML file is compressed, you need to make a small change to how you load it in. Simply change the URLLoader.dataformat to URLLoaderDataFormat.BINARY:

var loader:URLLoader	= new URLLoader;
loader.dataFormat	= URLLoaderDataFormat.BINARY;

// listen for our events
loader.addEventListener( Event.COMPLETE, this._onLoad );
loader.addEventListener( IOErrorEvent.IO_ERROR, this._onError );
loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, this._onSecurityError );
loader.load( new URLRequest( url ) );

Improvements

The XML Compressor/Uncompressor tool is written in Flash, quick and dirty. It does what I need it to do, so I've never extended it. If it was written in Flex or AIR, then there's a lot more you could do with it - you could probably get rid of the Current folder requirement, and enable the ability to drag and drop XML files from the explorer (much more conveniant that n00b buttons lak). If you have a penchant to try it, the source code is included below and free to do whatever you want with it.

If you're kind of crazy you can also try creating a XML editor :D Most of the time when I uncompress a compressed file, it's to add a simple value, so the ability to do that in the program could come in handy.

"It doesn't work!"

If you have problems with loading and saving files, it's probably a SecurityError. Head to http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html and make sure the folder where the XML compressor SWF file is located (not the XML files that you're going to load) is set to "Always trust". In the image below, my SWF file path is C:\Users\divillysausages\XMLCompressor.swf, so I can either explicitly trust the specific SWF file, or (as I've done here) trust the entire divillysausages folder, which will affect all SWFs in that folder (plus subfolders).

The Flash Player security settings dialog

Files

Comments

parpsies

?

Damian Connolly
parpsies

http://j.mp/gKrHCY - this is what i think of your comment moderation

flame
an ugly argument in a newsgroup
spam
annoying unrequested email
troll
someone who start flames by posting stupid things
Anonymous

Dude,

I downloaded your file.

I copied and pasted my link. I clicked Open File.

Nothing happened.

I went to Adobe. They say my link is fine..

Want to take another 5 seconds and re-explain to me what I "didn't" do?

Feel like I just wasted 10 minutes for nothing....It can't be THAT hard.

Damian Connolly

Hey,

Sorry you've been having trouble with the tool. I really need to get off my ass and make an AIR version. For your problems:

  • Download the SWF. Place it in, say, C:\Users\[YOUR_USER_NAME]\Documents.
  • Go to the Global Security Settings panel and make sure the folder that you've placed the SWF in (C:\Users\[YOUR_USER_NAME]\Documents) is set to Always trust. I've clarified this in the post above, as I think you might be trusting the folder where your XML file is located.
  • If your XML path is C:\Some Folder\myXML.xml, then paste C:\Some Folder\ into the Current Folder TextField in the XML compressor tool. If you navigate to the XML location in Windows Explorer, then click on the address bar, you should just be able to copy this directly (the tool will add the final "\")
  • Hit "Open File", browse to your file and load it. It should now work.

Most of these problems have to do with the restrictions placed on local SWF files, which is why I have to do an AIR version, as you'll just be able to drag and drop in that one.

Let me know if you're still having problems.

Submit a comment

* indicates required