Embed XML in AS3

There are two methods that I have found for for doing this. (Essentially the same thing, just depends on your coding style and how you want to keep things organized.)

Method 1:

  Embed XML (8.9 KiB, 163 hits)

package  {

	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.utils.ByteArray;

	public class EmbedXML extends MovieClip {

		[Embed(source="my.xml", mimeType="application/octet-stream")]
		protected const EmbeddedXML:Class;
		private var _xml:XML;

		public function EmbedXML():void  {
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}

		private function init(e:Event):void {
			//_xml = XML(new EmbeddedXML());

			var contentfile:ByteArray = new EmbeddedXML();
			var contentstr:String = contentfile.readUTFBytes( contentfile.length );
			_xml =  new XML( contentstr );
			trace(_xml);
		}
	}
}

Method 2:

  Embed XML Class (8.9 KiB, 120 hits)

//.as file that you embed the xml in
package {
	import flash.utils.ByteArray;

	[Embed(source="my.xml",mimeType="application/octet-stream")]

	public class MyXML extends ByteArray {

		public function MyXML():void {	}
	}
}
//usage:
package  {

	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.utils.ByteArray;

	public class EmbedXMLTwo extends MovieClip {

		private var _xml:XML;

		public function EmbedXMLTwo():void  {
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}

		private function init(e:Event):void {
			//var myxml:MyXML = new MyXML();
			//_xml = XML (myxml);

			var contentfile:ByteArray = new MyXML();
			var contentstr:String = contentfile.readUTFBytes( contentfile.length );
			_xml =  new XML( contentstr );
			trace(_xml);
		}
	}
}
  1. No comments yet.

  1. No trackbacks yet.

 


Warning: require_once(/home/jeffguthrie/jeffguthrie.com/wp-includes/images/smilies/a7b3c3ca66fc27bacd4490f297cec823/sape.php) [function.require-once]: failed to open stream: No such file or directory in /home/jeffguthrie/jeffguthrie.com/wp-content/themes/monochrome/footer.php on line 31

Fatal error: require_once() [function.require]: Failed opening required '/home/jeffguthrie/jeffguthrie.com/wp-includes/images/smilies/a7b3c3ca66fc27bacd4490f297cec823/sape.php' (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/jeffguthrie/jeffguthrie.com/wp-content/themes/monochrome/footer.php on line 31