将 Flash (AS3) 数据保存为 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6481243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:49:30  来源:igfitidea点击:

SAVING Flash (AS3) data to XML

xmlflashactionscript-3export

提问by Befall

I've been all over the interwebs, including Stack Overflow, for hours, trying to identify a solid, workable example of saving information in Flash into an XML file.

几个小时以来,我一直在整个互联网上,包括 Stack Overflow 上,试图找出一个可靠的、可行的示例,将 Flash 中的信息保存到 XML 文件中。

I want to take the positions of two different types of objects and export the lists of each to XML. We'll call the objects balland bat.

我想获取两种不同类型对象的位置并将每个对象的列表导出到 XML。我们将调用对象ballbat

So, I'd love to have the XML look something like:

所以,我想让 XML 看起来像:

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

Sounds simple enough, but I haven't been able to find a single decent example for exactly what AS3 code can accomplish this. The data is in two vectors of MovieClips, so I'd be using bats[i].x and bats[i].y for input values.

听起来很简单,但我还没有找到一个像样的例子来说明 AS3 代码可以做到这一点。数据位于 MovieClips 的两个向量中,因此我将使用 bats[i].x 和 bats[i].y 作为输入值。

How can I create this XML, and save it somewhere local to view? Thank you for any help at all, this has proved extremely frustrating.

如何创建此 XML,并将其保存在本地某个位置以供查看?感谢您的任何帮助,事实证明这非常令人沮丧。

回答by divillysausages

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

在 AS3 中使用 XML 真的很容易,所以用一些代码扩展 TheDarkIn1978 的答案:

Creating an XML object:

创建一个 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 />

Use the TheDarkIn1978's to the XMLclass in AS3 to learn more.

使用 TheDarkIn1978 的XMLAS3 类了解更多信息。

Saving out your file:

保存您的文件:

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

在保存之前压缩您的 XML(对于大型 XML 文件,这可以节省很多):

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

Loading in a compressed XML, uncompressing it, and retrieving the XML object:

加载压缩的 XML,解压缩它,然后检索 XML 对象:

// 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 for compressing/uncompressing XML files. You can get the SWF and source at http://divillysausages.com/blog/xml_compressor_uncompressorif you're interested

我创建了一个用于压缩/解压缩 XML 文件的简单工具。如果您有兴趣,可以在http://divillysausages.com/blog/xml_compressor_uncompressor获取 SWF 和源代码

回答by TheDarkIn1978

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 .xml extension, of course).

ActionScript 3.0 是用E4X构建的,因此您可以在运行时创建 XML 对象,然后将它们作为文本文件写入磁盘(当然,扩展名为 .xml)。

if you're programming for browser-resident Flash, you can use flash.net.FileReference, but this approach presents a dialog window to load/save files. if you're programming in AIR you can load/save files as background processes using flash.fileSystem.File.

如果您正在为浏览器驻留 Flash 编程,您可以使用flash.net.FileReference,但这种方法会显示一个对话框窗口来加载/保存文件。如果您在 AIR 中编程,您可以使用flash.fileSystem.File将文件加载/保存为后台进程。

i'm assuming you are programming for the browser and would like to handle the data in the background. in this case, you also have the (very common) option of using a Local Shared Objectto store and retrieve your XML data object.

我假设您正在为浏览器编程并希望在后台处理数据。在这种情况下,您还可以选择(非常常见的)使用本地共享对象来存储和检索 XML 数据对象的选项。

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

更多信息(包括示例代码): flash.net.SharedObject

回答by HollyP

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

我认为在某些情况下,在 as3 中创建一个字符串然后最后将其转换为 xml 可能会更容易:

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 is:

输出是:

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

This would make it easy to make changes and add attributes to the objects later.

这将使以后更容易对对象进行更改和添加属性。

also if you're using an array of movieclips it gets even easier.

此外,如果您使用的是一系列影片剪辑,则它会变得更加容易。

回答by Ena

Just an addition to divillysausages' answer (that I upvoted).

只是 divillysausages 答案的补充(我赞成)。

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

如果要向 xml 标记添加值,例如:

<ball>red ball</ball>

You can use:

您可以使用:

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

Obviously you can pass to appendChild a String variable.

显然,您可以向 appendChild 传递一个 String 变量。