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);
}
}
}
No comments yet.