且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

节能的Flash(AS3)数据到XML

更新时间:2022-06-20 02:57:29

与XML在AS3工作是很容易,所以要扩大TheDarkIn1978的一些code答案:

Working with XML in AS3 is really easy, so to expand on TheDarkIn1978's answer with some code:

创建XML对象:

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 />

使用的TheDarkIn1978的到 XML 类AS3了解更多信息。

Use the TheDarkIn1978's to the XML class in AS3 to learn more.

保存您的文件:

// 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

的COM $ P $保存前pssing你的XML(有大的XML文件,这样可以节省大量的):

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" );

装载在COM pressed XML,uncom pressing它,并检索XML对象:

Loading in a compressed XML, uncompressing it, and retrieving 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 );
}

我创建了COM pressing / uncom pressing XML文件的简单工具。你可以得到的SWF和源的http://divillysausages.com/blog/xml_com$p$pssor_uncom$p$pssor如果你有兴趣