jump to navigation

Embedding Binary XML July 28, 2010

Posted by Tournas Dimitrios in Actionscript.
trackback

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.

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s