Saving Flash Data (AS3) to XML

For several hours, I was everywhere in interwebs, including Stack Overflow, trying to define a reliable, workable example of storing information in Flash in an XML file.

I want to take the positions of two different types of objects and export the lists of each into XML. We will call the objects ball and bat .

So, I would like the XML to look something like this:

<objects> <ball xPos=34 yPos=43/> <ball xPos=12 yPos=94/> <bat xPos=1 yPos=39/> </objects> 

It sounds simple enough, but I could not find any worthy example for what exactly AS3 code can do. The data is in two MovieClips vectors, so I would use the [i] .x bit and the [i] .y bit for the input values.

How can I create this XML and save it somewhere locally for viewing? Thanks for any help, this turned out to be extremely unpleasant.

+4
source share
4 answers

Working with XML in AS3 is very simple, so deploy the response to TheDarkIn1978 using some code:

Creating an XML Object:

 var objs:XML = new XML( <objects /> ); // create the <objects /> node // for your objects var ball1:XML = new XML ( <ball /> ); // create the <ball /> node ball1.@xPos = 12; // add 12 as an attribute named "xPos" ball1.@yPos = 42; // add 42 as an attribute named "yPos" objs.appendChild( ball1 ); // add the <ball> node to <objects> // an example of using variables in your xml var name:String = "something"; var sx:XML = new XML( <{name} /> ); // creates a node <something /> 

Use TheDarkIn1978 for the XML class in AS3 to learn more.

File Saving:

 // saving out a file var f:FileReference = new FileReference; f.save( sx, "myXML.xml" ); // saves under the name myXML.xml, "sx" being your root XML node 

Compressing your XML before saving (with large XML files this can save a lot):

 // compressing before saving var f:FileReference = new FileReference; var bytes:ByteArray = new ByteArray; bytes.writeUTFBytes( myXML ); // "myXML" being your root XML node bytes.compress(); // compress it f.save( bytes, "myXML.xml" ); 

Download in compressed XML, extract and extract the XML object:

 // uncompressing a compressed xml var loader = new URLLoader; loader.dataFormat = URLLoaderDataFormat.BINARY; // listen for our events loader.addEventListener( Event.COMPLETE, this._onLoad ); loader.addEventListener( IOErrorEvent.IO_ERROR, this._onFail ); // not shown loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, this._onSecurityError ); // not shown private function _onLoad( e:Event ):void { var loader:URLLoader = e.target as URLLoader; // get the data as a bytearray var ba:ByteArray = loader.data as ByteArray; // uncompress it try { ba.uncompress(); } catch ( e:Error ) { trace( "The ByteArray wasn't compressed!" ); } // get our xml data myXML = XML( ba ); } 

I created a simple tool to compress / decompress XML files. You can get the SWF and source at http://divillysausages.com/blog/xml_compressor_uncompressor if you are interested

+17
source

ActionScript 3.0 is built with E4X , so you can create XML objects at runtime and then write them to disk as text files (with the .xml extension, of course).

If you are programming for a resident Flash browser, you can use flash.net.FileReference , but this approach presents a dialog box for loading / saving files. if you program in AIR, you can load / save files as background processes using flash.fileSystem.File .

I assume that you are programming for the browser and want to process the data in the background. in this case, you also have the (very common) option of using a local shared object to store and retrieve the XML data object.

here (including sample code): flash.net.SharedObject

+2
source

I think in some cases it might be easier to just create a string in as3 and then pass it as xml at the end:

 import flash.geom.Point; var cordiantes = [new Point(34,12),new Point(14,2), new Point(10,15)]; var objects = ["ball","ball","bat"]; // starting the object node. var myXmlStr = "<objects>"; for(var i = 0; i < objects.length; i++) { // adding things to the string between the object nodes. myXmlStr += "<" + objects[i] + " xPos= '" + cordiantes[i].x + "' yPos='" + cordiantes[i].y + "' />"; } // closing the objects node. myXmlStr += "</objects>"; // turning the string into xml. var xmlfinal:XML = XML(myXmlStr); trace(xmlfinal); 

Output:

 <objects> <ball xPos="34" yPos="12"/> <ball xPos="14" yPos="2"/> <bat xPos="10" yPos="15"/> </objects> 

This will make it easier to make changes and add attributes to objects later.

also, if you use an array of movie clips, it becomes even easier.

+1
source

Just an addendum to the answer to divillysausages (which I supported).

If you want to add a value to the xml tag, for example:

 <ball>red ball</ball> 

You can use:

 var ball:XML = <ball />.appendChild("red ball"); 

Obviously, you can pass the String variable to appendChild.

0
source

All Articles