0

I am playing around with saving .xml files from data in my game. I know how to convert an xml object to a ByteArray and write that to a FileReference. What I need to know now is how to go from an empty xml object (using var xml:XML = new XML()) to a fully populated xml object.

Can someone show me what functions to use to add nodes and values? Specifically I need to know how to make several of the same type of node. Here's a sample of what the final xml should look like:

<data>
    <player>
        <money>15831</money>
        <shipType>1</shipType>
        <ship>
            <fuelCur>450</fuelCur>
            <energyCur>160</energyCur>
            <shieldCur>100</shieldCur>
            <armorCur>40</armorCur>
            <structCur>15</structCur>
        </ship>
    </player>
    <currentSystem>3</currentSystem>
    <currentPlanet>3</currentPlanet>
    <date>
        <year>2012</year>
        <month>10</month>
        <day>15</day>
    </date>
    <missionFlag>true</missionFlag>
    <planet id="0">
            <population>0</population>
    </planet>
    <planet id="1">
            <population>29827632</population>
    </planet>
    <planet id="2">
            <population>0</population>
    </planet>
</data>

It might be useful to know how to remove and how to alter nodes and values, too.

Also, do I need to import something from the flash library to do this with XML? I'm using FlashDevelop, but when I type xml., FlashDevelop doesn't bring up suggestions like it normally does.

2 Answers 2

7

Editing XML in AS3 is actually pretty easy and pretty basic - it's a core part of AS3. AS3 automatically adds nodes as you call them, like so:

var xml:XML = <data />;
xml.player.money = 15831;
xml.player.shiptype = 1;
xml.player.ship.fuelCur = 450;

Will result in:

<data>
    <player>
        <money>15831</money>
        <shiptype>1</shiptype>
        <ship>
            <fuelCur>450</fuelCur>
        </ship>
    </player>
</data>

Then to add multiples of the same node, just start a new XML object of the type you want, and append it to the XML object you're working on. Or skip the separate xml object entirely. Repeat as many times as needed:

var segment:XML = <planet />;
xml.appendChild(segment); // xml.planet[0]
xml.appendChild(segment); // xml.planet[1]
xml.appendChild(<planet />); // xml.planet[2]
//etc...

Then you can add values to them by their indexes.

//Assuming this is the 4th planet you've added...
xml.planet[3].population = 29827632;
Sign up to request clarification or add additional context in comments.

3 Comments

ha, all this time i've just been using appendChild() - i didn't know you could attach new nodes using dot syntax :D
There are not only plenty of tutorials, but if I type 'as3' in the Google search box, the 4th item in the drop-down is 'as3 xml'. Here are a few example tutorials: ex1, ex2, ex4, ex4
@wvxvw - Mentioning that XML is a core data type in AS3 did more to help my understanding than the other comments on this question. Thanks!
0

In FlashDevelop, try creating a separate XML file entirely. That will give you better code prompting.

For XML, though, I have found that Adobe's rudimentary docs on their XML class has a wealth of useful examples and tools for creating and using XML objects.

8 Comments

How would I make a separate xml file? And how would that help with code prompting?
I hate to not answer a question, but this is really basic FlashDevelop and AS3 info. I think the best answers would be found by Googling "as3 load xml tutorial" and "FlashDevelop create new XML file".
I've googled around, and haven't found any particularly clear tutorials. kirupa.com/developer/actionscript/create_edit_xml.htm was one I found that seemed like what I needed, but several of the functions aren't even in the XML model from what I can tell.
His code seems ok. I am not being critical when I say this, but I think you need to find some intro tutorials to AS3 implementation of XML, and actually build some programs from them. If you are new to AS3, then start with the "Hello, World!" kind of stuff, and move on up.
@iND i actually think he's trying to create XML from within flash, rather than separately (useful for example, if you're making a level editor)
|

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.