Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I threw this together, and sure enough you can save to .XML using the following as a minimalist example.</p> <pre><code>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= &lt;xml&gt; &lt;test&gt;data&lt;/test&gt; &lt;/xml&gt;; } 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'); } } </code></pre> <p>}</p> <p>There are some things to note.</p> <ul> <li>You require Flash Player 10 to use the save method of the <code>FileReference</code> class.</li> <li>In order to do anything that INVOKES a prompt, Flash requires user interaction like keyboard or mouse input.</li> </ul> <p>In the above I listen for <code>MouseEvent.MOUSE_DOWN</code> on the stage to serve as the USER INTERACTION which is required to invoke the save prompt.</p> <p>I setup a basic XML structure within the code (this will typically come from and external source and will work fine both ways.</p> <p>A <code>ByteArray</code> is created and the XML is written to the <code>ByteArray</code>.</p> <p>The save method of the <code>FileReference</code> class requires a <code>ByteArray</code> and default save name be passed as the two parameters.</p> <p>I hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload