Reputation: 8125
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.
Upvotes: 0
Views: 7776
Reputation: 8125
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;
Upvotes: 7
Reputation: 2649
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.
Upvotes: 0