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.
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:
Will result in:
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:
Then you can add values to them by their indexes.