GhostWire Studios - Flash/Flex UI Components Development And Consulting Services
Quality User Interface Controls For Flash Application DevelopmentAspireUI Components

Dec 04 2009

[AS3] Embedding Binary XML

Published by at 6:12 am under Flash,Flash AS3,Tips

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.

pixelstats trackingpixel
Share or Bookmark This Post:
  • StumbleUpon
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Live
  • Yahoo! Buzz
  • Netvibes
  • NewsVine
  • Reddit
  • Slashdot
  • Technorati
  • BlinkList
  • Mixx
  • Diigo
  • Faves
  • Suggest to Techmeme via Twitter
  • Twitter

Other Posts You Might Enjoy:

       

2 responses so far

2 Responses to “[AS3] Embedding Binary XML”

  1. Martinon 07 Dec 2009 at 9:37 am

    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

  2. Sharedtuton 02 Feb 2010 at 4:41 am

    this seems complicated but is very easy once you play with the buttons, thank you

Trackback URI | Comments RSS

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word