There are lots of articles to demonstrate how to embed the XML file in Flex and Actionscript 3. But it takes me almost 30 minutes to try and none of them works for me. Finally, I write my own test code and find the right way to embed the XML file in Flex with Actionscript 3, and access the data with XML object. I hope it will help you and save your time.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" creationComplete="onCreationComplete(event)">
	<mx:Script>
		<!&#91;CDATA&#91;
			&#91;Embed(source="demo.xml")&#93;
			protected var EmbeddedXML:Class;	
			
			private function onCreationComplete(event:Event):void
			{
				//doesn't work
				//var a:XML = EmbeddedXML as XML;
				//trace("a: " + a.toXMLString());
				
				//works
				var b:XML = new XML(EmbeddedXML.data);
				trace("b: " + b.toXMLString());
				
				//doesn't work
				var c:XML = XML(new EmbedXML());
				trace("c: " + c.toXMLString());
				
				//doesn't work
				var d:XML = new XML(new EmbedXML());
				trace("d: " + d.toXMLString());
				
				//works
				var e:XML = EmbeddedXML.data as XML;
				trace("e: " + e.toXMLString());
				
				//doesn't work
				//var f:XML = new EmbeddedXML() as XML;
				//trace("f: " + f.toXMLString());
			}
		&#93;&#93;>
	</mx:Script>
</mx:Application>
Previous PostNext Post

Leave a Reply

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