Embedding XML in your Flex application
When you want to embed resources in a Flex application you just use the [Embed] tag as advised by Adobe documentation.
However this section of the developer’s guide is saying that only images, Flash files, audio and font resources can be embedded that way. More exactly other resources can be embedded but they end up being exposed as ByteArray which is a bit complex to process when you want to embed something as simple as an XML file.
So what is the easiest way to embed an XML resource in your application? Just use the following tag in you MXML:
<mx:XML id="xmlSource" source="data.xml"/>
then you can access xmlSource as an XML object and process it using E4X.
Maybe the Adobe documentation is missing a central place where all embedding mechanisms are described, not just [Embed], or maybe it already exist and I’ve missed it ![]()








July 16th, 2008 at 12:33 pm
You can still use the Embed tag, but its a little wonky:
http://www.peterjoel.com/blog/index.php?archive=2006_05_01_archive.xml#114770134881719072
July 16th, 2008 at 4:14 pm
Embed,
Instead of using ByteArray, mimeType and whatever you can do that:
[Embed(source=’/aXml.xml’)]
public var xmlSource:Class;
And get the xml doing this:
var xml:XML = XML(xmlSource.data);
VELO
July 16th, 2008 at 7:28 pm
I was actually try to do this very thing this morning. I eventually came across this post on dispatchEvent.org
http://dispatchevent.org/roger/embed-almost-anything-in-your-swf/
The technique described in the entry didn’t work for me, but rudestar’s comment did the trick.
package
{
import flash.display.MovieClip;
import flash.utils.ByteArray;
public class Test extends MovieClip
{
[Embed(source=”data.xml”, mimeType=”application/octet-stream”)]
private static const MyData:Class;
public function Test():void{
var byteArray:ByteArray = new MyData() as ByteArray;
var xml:XML = new XML(byteArray.readUTFBytes(byteArray.length));
trace(xml.toXMLString());
}
}
}