Compiling XML Data into your SWF

Have you ever wanted to actually compile XML data into your SWF before? I wanted to do this to embed the build version into my Flex appliction. The version was stored in an XML file and updated during the build process. But I wanted the version to actually exist inside the SWF in order to decouple the depandancy on the external XML file at runtime. This way I don’t need to worry about the XML file not loading or getting seperated from the SWF. The version will always be there. Here is a simple example doing this in Flex.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<mx:Text xmlns:mx="http://www.adobe.com/2006/mxml" text="{version.@release.toString()}">
    <mx:Script>
        <![CDATA[
            [Embed(source="/version.xml", mimeType="application/octet-stream")]
            private var VersionFile:Class;
 
            private var bytes:ByteArray = new VersionFile() as ByteArray;
 
            [Bindable]
            private var version:XML = new XML(bytes.readUTFBytes(bytes.length));
        ]]>
    </mx:Script>
</mx:Text>

As you can see in the above code example, I’m embedding the XML file as an octet-stream and reading it in as a ByteArray. Then I simply read the UTF Btyes out of the ByteArray and pass that string into the constructor of the XML object. Now I can treat this just like any other XML data in ActionScript 3. In this Flex example I’m binding the XML to the text property of this Text component.

Below is an example of the XML data file that I’m embeding:

1
2
<?xml version="1.0" encoding="utf-8"?>
<version release="v1.0.1" />

2 thoughts on “Compiling XML Data into your SWF

  1. Scott

    Can you outline a little what was involved in your build process – such as what was performing the update to the build #?

    Thx!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *