Back in February I blogged about how to embed arbitrary file types in ActionScript (see “Embedding assets with application/octet-stream” for the complete post). That post was inspired by some work I was doing at the time which involved embedding XML files in ActionScript as an alternative to using the <mx:Model> tag in mxml. In the time since I have created a simple yet useful class which can be utilized as both a utility a concrete or a base class to facilitate decoding an embedded XML file to an XML object.
XMLAsset
provides an API which allows for the decoding of an embedded XML file (via the [Embed] metadata tag / compiler directive) to a native XML object. There are three different ways XMLAsset can be implemented, the first of which is to simply create an instance of XMLAsset and pass the constructor a reference to an embedded XML asset as follows:
1 2 3 4 5 6 7 8 9 | import com.ericfeminella.xml.XMLAsset; [Embed("config.xml")] private static const Asset:Class; var config:XMLAsset = new XMLAsset( Asset ); trace( config.xml.toString() ); |
In the above example we are simply creating an instance of XMLAsset and passing a reference to an embedded XML asset from which to decode to a native XML object. From there we can access the decoded xml object as needed.
The second implementation is to sub-class XMLAsset and pass in a reference to a specific embedded xml file by calling super on XMLAsset, as can be seen in the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package { import com.ericfeminella.xml.XMLAsset; public class Config extends XMLAsset { [Embed("config.xml")] private static const Asset:Class; public function Config() { super( Asset ); } } } // client imlementation: var config:Config = new Config(); trace( config.xml.toString() ); |
In the above example we are simply extending XMLAsset and passing a reference of the embedded asset to super. From there we can access the decoded xml object as needed.
The third way to utilize XMLAsset is to invoke the static createXML
method directly. To do so we pass in an embedded XML asset just as we did in the first to examples. The createXML
method returns a native XML object. An example is as follows:
1 2 3 4 5 6 7 8 | // client imlementation: [Embed("config.xml")] private static const Asset:Class; var config:XML = XMLAsset.createXML( Asset ); trace( config.toString() ); |
One thing to keep in mind is if the file which is to be embedded contains XML but does not have the recognized .xml extension the compiler will throw an error. In such cases you need only specify the mimeType as “application/octet-stream†in order to embed the file without error, however I suggest always using the .xml extension whenever possible as in the event the file should contain invalid XML mark-up you will get an error during compilation rather than at runtime. In any case embedding an XML file with an extension other than .xml is very simple, an example of which can be seen in the following:
1 2 3 4 5 6 7 8 | // client imlementation: [Embed("app.config", mimeType="application/octet-stream")] private static const asset:Class; var config:XML= XMLAsset.createXML( asset ); trace( config.toString() ); |
And that’s all there is to it. Enjoy.
There is another way. This requires a bit different implementation of XMLAsset. From the top of my head:
package com.ericfeminella.xml.XMLAsset
{
import flash.utils.ByteArray;
public class XMLAsset extends ByteArray
{
private var _xml:XML;
public function get xml():XML
{
if (!_xml)
{
position = 0;
_xml = new XML(readUTFBytes(bytesAvailable));
}
return _xml;
}
}
}
package
{
import com.ericfeminella.xml.XMLAsset;
[Embed(“config.xml”, mimeType=”application/octet-stream”)]
public class Config extends XMLAsset
{
}
}
var config:Config = new Config();
trace( config.xml.toXMLString() );
Greetz Erik