Nov 07 2009
[AS3] Simple Editor For Loading And Saving XML As Binary
This post is a supplement to “Saving XML As Binary”.
As mentioned in the previous post, you can save XML in binary and get the benefits of compression, but doing so would make it impossible to edit the data through a text editor.
This post explores a simple tool you can create easily using Flash/ActionScript 3, a tool that will allow you (or your clients and end-users) to load, read, edit and save XML in compressed binary format. As shown below, this is a minimalist approach – feel free to beautify and/or customize it to fit your own requirements.
You can find the source code at the end of this post. The code does not use any UI component, not from Aspire UI, Flex or Flash, just plain native Flash Player classes so you can compile the AS3 class alone without any additional library.
(480×480 SWF, 2KB)
- You need Flash Player 10+ to view the above SWF.
- Click the “Browse” button to open a native local filesystem dialog – choose an XML file to load.
- Once loaded, the XML will be displayed in the input text field above.
- After that, click the “Save” button to open a native local filesystem dialog – choose location and file name.
- Look for the saved file in your local storage – notice that the file size is much smaller than the original.
- If you open the saved file in a text editor, you will see garbage text because it is in compressed binary format.
- You can load a binary XML file into the above tool (if it was saved in “.xml” extension).
- You can edit the XML in the text field before saving.
BinaryXMLTool Class
package { import flash.display.Sprite; import flash.display.Stage; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.MouseEvent; import flash.events.IOErrorEvent; import flash.net.FileFilter; import flash.net.FileReference; import flash.net.URLLoader; import flash.net.URLRequest; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFieldType; import flash.text.TextFormat; import flash.utils.ByteArray; import flash.xml.XMLDocument; public class BinaryXMLTool extends Sprite { // ** minimalist buttons ** private var browseButton:TextField; private var saveButton:TextField; // ** minimalist editor ** private var xmlText:TextField; // ** browse/load/save ** private var xmlFile:FileReference; public function BinaryXMLTool():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point stage.align = StageAlign.LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; stage.showDefaultContextMenu = false; // ** draw minimalist text editor ** xmlText = new TextField(); xmlText.multiline = true; xmlText.background = true; xmlText.backgroundColor = 0xEEEEEE; xmlText.type = TextFieldType.INPUT; xmlText.width = 400; xmlText.height = 400; xmlText.border = true; xmlText.x = (stage.stageWidth - xmlText.width) * 0.5; addChild(xmlText); // ** draw minimalist browse button ** browseButton = new TextField(); browseButton.autoSize = TextFieldAutoSize.LEFT; browseButton.background = true; browseButton.backgroundColor = 0x000000; browseButton.defaultTextFormat = new TextFormat("Tahoma", 14, 0xFFFFFF, true, null, null, null, null, null, 4, 4); browseButton.selectable = false; browseButton.text = "BROWSE"; browseButton.x = (stage.stageWidth - browseButton.width) * 0.5; browseButton.y = 420; addChild(browseButton); // ** draw minimalist save button ** saveButton = new TextField(); saveButton.autoSize = TextFieldAutoSize.LEFT; saveButton.background = true; saveButton.backgroundColor = 0xAAAAAA; saveButton.defaultTextFormat = browseButton.defaultTextFormat; saveButton.selectable = false; saveButton.text = "SAVE"; saveButton.x = (stage.stageWidth - saveButton.width) * 0.5; saveButton.y = 460; addChild(saveButton); // ** button listeners ** browseButton.addEventListener(MouseEvent.CLICK, on_buttonClick, false, 0, true); saveButton.addEventListener(MouseEvent.CLICK, on_buttonClick, false, 0, true); } /** * handle browse or save */ private function on_buttonClick(evt:MouseEvent):void { var btn:TextField = evt.target as TextField; if (btn) { if (btn.text == "BROWSE") { xmlFile = new FileReference(); xmlFile.addEventListener(Event.SELECT, on_xmlSelect, false, 0, true); xmlFile.browse([new FileFilter("XML Documents","*.xml")]); } else if (btn.text == "SAVE") { if (xmlFile) { if (xmlText.text.length) { // ** saving as binary ** var data:ByteArray = new ByteArray(); data.writeUTFBytes(xmlText.text); data.compress(); new FileReference().save(data, "bin" + xmlFile.name); } } } } } /** * handle browse, load XML file */ private function on_xmlSelect(evt:Event):void { xmlFile.removeEventListener(Event.SELECT, on_xmlSelect); xmlFile.addEventListener(Event.COMPLETE, on_xmlComplete, false, 0, true); xmlFile.load(); } /** * handle load, check if it is binary, uncompress, display XML in editor */ private function on_xmlComplete(evt:Event):void { xmlFile.removeEventListener(Event.COMPLETE, on_xmlComplete); saveButton.backgroundColor = 0x000000; var data:* = FileReference(evt.target).data; if (data is ByteArray) { try { ByteArray(data).uncompress(); } catch(e:Error) { } } data = XML(data); xmlText.text = data; } } }
See Also:
- [AS3] Saving XML As Binary
This could be useful if you have an external large, verbose XML... - [AS3] Applying ROT128 Encryption On Binary XML
This is Part III of our discussion on ROT128 Encryption. Part I:... - [AS3] Applying ROT128 Encryption On Embedded/Module SWFs
This post is a supplement to “Applying ROT128 Encryption On ByteArray”. Some... - [AS3] Avoiding NULL Object Reference Error When Loading Module SWF
When building modular Flash applications, a common error that may be encountered... - Loading embedded fonts on-demand using the uiFonts manager
Why Embed Fonts? Embedding a font inside a SWF file means that...

sunny, this tool works great! it reduced my XML 90% 50K => 5K.
Nice trick – I suppose the result is similar to binary embed XML but easier to update
Nice tool! It allows me to load, read, edit and save XML in compressed binary format. Great! Thanks for sharing!
this app would be far more useful if it allowed to flash-compress any binary file (not just xml)
@makc, er… you missed the point of the post?