保存在XML文件中AS3可能 [英] Saving XML file in AS3 is possible

查看:106
本文介绍了保存在XML文件中AS3可能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var xml:XML = <myXml>
                    <item prop="1" />
                    <item prop="2" />
                </myXml>;

我需要保存在本地硬盘(项目目录)的XML文件。

I need to save as xml file in local harddisk(project directory).

是否有可能保存在AS3本身?

Is it possible to save in as3 itself?

推荐答案

我扔了一起,果然可以节省使用以下作为一个极简的例子来.XML。

I threw this together and sure enough you can save to .XML using the following as a Minimalist example.

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= <xml>
			  <test>data</test>
		      </xml>;
	}
	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');
	}
}

}

有一些事情要注意。

  • 您需要的Flash Player 10使用保存FileReference类的方法。
  • 在为了做任何事情,调用提示,闪存需要像键盘或鼠标输入的用户交互。

在上面我听的MouseEvent.MOUSE_DOWN在舞台上,作为这是需要调用保存提示用户交互。

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.

我的设置中的code一个基本的XML结构(这通常来自外部来源,将正常工作两者兼得。

I setup a basic XML structure within the code (this will typically come from and external source and will work fine both ways.

一个ByteArray将创建的XML写入ByteArray中。

A ByteArray is created and the xml is written to the ByteArray.

FileReference类的保存方法需要将ByteArray默认保存的名称被传递的两个参数。

The save method of the FileReference class requires a ByteArray and default save name be passed as the two parameters.

我希望这有助于。

布赖恩·霍奇
blog.hodgedev.com hodgedev.com

这篇关于保存在XML文件中AS3可能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆