作者:ckx1989 | 来源:互联网 | 2023-05-30 11:10
var xml:XML =
;
I need to save as xml file in local harddisk(project directory).
我需要在本地硬盘(项目目录)中保存为xml文件。
Is it possible to save in as3 itself?
是否可以保存as3本身?
3 个解决方案
I threw this together, and sure enough you can save to .XML using the following as a minimalist example.
我把它扔在一起,当然你可以使用以下作为极简主义的例子保存到.XML。
package com.hodgedev.xmlcreator
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.ByteArray;
import flash.net.FileReference;
/**
* ...
* @author Brian Hodge (brian@hodgedev.com)
*/
public class Main extends Sprite
{
private var _xml:XML;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//Calling the save method requires user interaction and Flash Player 10
stage.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
_xml=
data
;
}
private function _onMouseDown(e:MouseEvent):void
{
var ba:ByteArray = new ByteArray();
ba.writeUTFBytes(_xml);
//ba.
var fr:FileReference = new FileReference();
fr.addEventListener(Event.SELECT, _onRefSelect);
fr.addEventListener(Event.CANCEL, _onRefCancel);
fr.save(ba, "filename.xml");
}
private function _onRefSelect(e:Event):void
{
trace('select');
}
private function _onRefCancel(e:Event):void
{
trace('cancel');
}
}
}
There are some things to note.
有一些事情需要注意。
- You require Flash Player 10 to use the save method of the
FileReference
class.
您需要Flash Player 10才能使用FileReference类的save方法。
- In order to do anything that INVOKES a prompt, Flash requires user interaction like keyboard or mouse input.
为了做任何隐藏提示的事情,Flash需要用户交互,如键盘或鼠标输入。
In the above I listen for MouseEvent.MOUSE_DOWN
on the stage to serve as the USER INTERACTION which is required to invoke the save prompt.
在上面我监听舞台上的MouseEvent.MOUSE_DOWN作为调用保存提示所需的USER INTERACTION。
I setup a basic XML structure within the code (this will typically come from and external source and will work fine both ways.
我在代码中设置了一个基本的XML结构(这通常来自和外部源,并且可以在两种方式下正常工作。
A ByteArray
is created and the XML is written to the ByteArray
.
创建一个ByteArray,并将XML写入ByteArray。
The save method of the FileReference
class requires a ByteArray
and default save name be passed as the two parameters.
FileReference类的save方法需要ByteArray和默认保存名称作为两个参数传递。
I hope this helps.
我希望这有帮助。
I'm sorry, your question isn't very clear.
对不起,你的问题不是很清楚。
Are you asking if you can save a file to the hard drive from within a compile SWF written in AS3?
您是否在询问是否可以从AS3编写的编译SWF中将文件保存到硬盘驱动器中?
Or are you asking if you can include a raw XML file in your AS3 project without needing to write it out as a variable?
或者您是否在询问是否可以在AS3项目中包含原始XML文件而无需将其作为变量写出来?
If you meant the former, no -- not without Adobe AIR. You can save data locally as a SharedObject, but not as an arbitrary file in the file system.
如果你的意思是前者,不是 - 不是没有Adobe AIR。您可以将数据本地保存为SharedObject,但不能作为文件系统中的任意文件保存。
If the latter, then yes -- you must embed the file just as you would embed another resource (such as an image or a sound). However, it looks like there might be a bug in Flash that makes this non-trivial to figure out how to do.
如果是后者,那么是 - 您必须像嵌入其他资源(例如图像或声音)一样嵌入文件。但是,看起来Flash中可能存在一个错误,这使得如何解决这个问题变得非常重要。
This link might be of help to you.
此链接可能对您有所帮助。
[Embed(source='../../../../assets/levels/test.xml', mimeType="application/octet-stream")]
public static const Level_Test:Class;
And then to parse the XML:
然后解析XML:
var ba:ByteArray = (new Levels.Level_Test()) as ByteArray;
var s:String = ba.readUTFBytes( ba.length );
xml = new XML( s );
Apologies if neither of those questions are what you were actually asking.
如果这些问题都不是你实际问的问题,请道歉。
Cheers!