Dec 04 2009
[AS3] Embedding Binary XML
This post is a supplement to “Saving XML As Binary”. I neglected to mention in that post that you can, if so desired, embed the binary XML within your SWF instead of loading it during run-time. That means you still get to keep the XML externally, not as part of your application code, so that the code and data can still be kept separate and maintained more easily.
However, it must be mentioned that embedding a text XML within SWF will get it compressed as part of the SWF compression anyway. Therefore, if compression is the only motivation, don’t convert the XML to binary and embed the binary version – it does not make sense. But if you wish to do some data encryption, then the additional work may be appropriate.
Embedding External Data
You can embed any external source, including any external XML file, within your SWF using the [Embed] tag:
// ActionScript 3.0 // assuming you have an XML file named "data.xml" in folder "assets/data/" [Embed(source="assets/data/data.xml", mimeType="application/octet-stream")] private static const binaryXML:Class;
Back To XML
You can then instantiate the binary data and convert it back to XML in your code:
var xmlData:XML; var bytes:ByteArray = new binaryXML() as ByteArray if (bytes) { try { bytes.uncompress(); } catch(e:Error) { } xmlData = XML(bytes); }
NOTE: The above code will be able to handle both binary XML and text XML files.
ROT128 Encrypted Binary XML
If you had performed ROT128 encryption on the binary XML, you decrypt it accordingly before calling uncompress():
var xmlData:XML; var bytes:ByteArray = new binaryXML() as ByteArray if (bytes) { // BEGIN ROT128 var j:int = data.length; while (j--) { data[j] += 128; } // END ROT128 bytes.uncompress(); xmlData = XML(bytes); }
NOTE: The above code assumes the data is binary and ROT128 encrypted; it will not be able to handle text XML.








thank you for this follow up on my comment in the previous post.
I agree with you that it is overkill. But every little extra confusion thrown in to make the highscore more unreachable for hackers is a good thing.
But the module-swf-binary-hack of yours is working quite well really.
Now I just have to make my external questions xml file to be in binary and having the wrong data in it – just so to confuse hackers to believe that when they have hacked the external data file – that it would give them the answers.
Thank you so much
this seems complicated but is very easy once you play with the buttons, thank you