Skip to main content
Inspiring
April 24, 2013
Answered

Overwrite XML data Using AS3

  • April 24, 2013
  • 1 reply
  • 5317 views

I have a file called temp.xml saved out on localhost (I'm running wamp for testing), and it has this in it:

<?xml version="1.0" encoding="utf-8"?>

<1>

<SAVEDATA pos="1"/>

</1>

The idea is, that as you get to checkpoints pos="" would be updated. I can't seem to figure out how to actually edit this. The reason SAVEDATA is within <1> is because I also wanted it to somehow assign a unique ID to each player (There's a login system), and when you login, it checks your ID, and continues the game from where you left off.

This topic has been closed for replies.
Correct answer kglad

if you use the xml like i showed, you can use the following to load and edit the pos attribute:

// load it

var urlLoader:URLLoader = new URLLoader();

urlLoader.addEventListener(Event.COMPLETE,f);

urlLoader.load(new URLRequest("test2.xml"));

var xml:XML;

function f(e:Event):void{

xml = XML(e.target.data);

}

// edit xml:  (for example to change player with id="3" to have pos="7", use editXMLF(3,7);

// you don't need to change any code above or below except the parameters you pass when you call editXMLF

function editXMLF(playerID:int, newPOS:int):void{

xml.player.(@id==playerID.toString()).SAVEDATA.@pos = newPOS;

}

1 reply

kglad
Community Expert
Community Expert
April 24, 2013

do you want to add the SAVEDATA node or edit its pos attribute?

p.s. that xml should have a wrapper node and you should not use a number for a node name.  use something like:

<?xml version="1.0" encoding="utf-8"?>

<players>

<player id="1">

<SAVEDATA pos = "1"/>

</player>

</players>

Inspiring
April 24, 2013

I want to edit the pos attribute, and thanks for the heads up.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 24, 2013

if you use the xml like i showed, you can use the following to load and edit the pos attribute:

// load it

var urlLoader:URLLoader = new URLLoader();

urlLoader.addEventListener(Event.COMPLETE,f);

urlLoader.load(new URLRequest("test2.xml"));

var xml:XML;

function f(e:Event):void{

xml = XML(e.target.data);

}

// edit xml:  (for example to change player with id="3" to have pos="7", use editXMLF(3,7);

// you don't need to change any code above or below except the parameters you pass when you call editXMLF

function editXMLF(playerID:int, newPOS:int):void{

xml.player.(@id==playerID.toString()).SAVEDATA.@pos = newPOS;

}