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

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

Is it possible to save in as3 itself?

1
  • Your question wasn't very clear, but the answer was just what I needed. Thanks! Commented Dec 29, 2011 at 5:09

3 Answers 3

17

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 ([email protected])
 */
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');
    }
}

}

There are some things to note.

  • You require Flash Player 10 to use the save method of the FileReference class.
  • In order to do anything that INVOKES a prompt, Flash requires user interaction like keyboard or mouse input.

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.

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

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

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

I hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

I know this post is old, but this is exactly the answer I needed except, how do write the xml using a for loop? E.g. I have a 2D array containing data of textures. I want to create a collision xml map based on the textures xml map. Like, how do you do: var xml:XML = <data> /*for loop*/ <data>?
use the for loop to build the actual xml, then send her off! var s:String; //Ommit the following, if you wish, flash does not use it, but if the file is shared between front and backend, you may wish to have that tag.
s = "<?xml version="1.0" encoding="utf-8">"; s.appendText("<data>"); //Feel free to loop how you wish e.g. for(var i:int; i < someValue; i++); I will use for each for each(var someObj in SomeOtherObj) { s.appendText("<collision id=" + someObj.id + " />"); } s.appendText("</data>"); This is obviously generic, but should get you going. var xml:XML = s;
Is there any way to save XML without dialog box ?
3

if you want to store it locally (on the client PC) , you can use a local shared object. Refer to this tutorial

Comments

1

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?

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?

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.

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.

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:

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.