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!
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).
Comments
?
http://bit.ly/eydAMK http://bit.ly/heOJV9 :D
http://j.mp/gKrHCY - this is what i think of your comment moderation
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.
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:
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