Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Overwrite XML data Using AS3

Explorer ,
Apr 24, 2013 Apr 24, 2013

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.

TOPICS
ActionScript
5.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 24, 2013 Apr 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

funct

...
Translate
Community Expert ,
Apr 24, 2013 Apr 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 24, 2013 Apr 24, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2013 Apr 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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 24, 2013 Apr 24, 2013

Wouldn't it be:

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

because player is within players.

Regardless, it isn't working for me.

player.addEventListener(MouseEvent.CLICK, addNumber);

function addNumber(Mouse:Event):void

{

          editXMLF(2, 4);

          trace("gfrvsg");

}

player is an object on stage, and the trace was simply to make sure I was clicking it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2013 Apr 24, 2013

no, it should be exactly as i showed.  the wrapper node is assigned to the variable xml. 

and what makes you think it's not working?  ie, what did you do to test?  (hint: use a trace(xml) statement to confirm it's working.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 24, 2013 Apr 24, 2013

I used a mouse click function on my player and the xml never updated.

I also tried trace(xml), and it returned "null"

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2013 Apr 24, 2013

copy and paste the code you're using.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 25, 2013 Apr 25, 2013

//XML Loading

var urlLoader:URLLoader = new URLLoader();

urlLoader.addEventListener(Event.COMPLETE,f);

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

var xml:XML;

function addNumber(Mouse:Event):void

{

          editXMLF(2, 4);

          trace("gfrvsg");

}

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;

}

trace(xml);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 25, 2013 Apr 25, 2013

1. nothing's calling addNumber.  therefore, nothing's calling editXMLF.

2. your trace(xml) should return null because it's executing before, temp.xml completes loading and xml is defined.

p.s. i'm  not trying to give you a hard time.  but, the code i suggested works.  you're not using it correctly.  almost certainly, you're trying to use editXMLF before your xml file is defined or, you're trying to edit a player id that doesn't exist in your xml.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 25, 2013 Apr 25, 2013

I had this on the top of my file (after imports and stop();)

player.addEventListener(MouseEvent.CLICK, addNumber);

I just didn't include it.

Now it changes the value in flash, but it's not saving to file.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 25, 2013 Apr 25, 2013

so, as long as you click that after loading is complete (highly likely), the code i suggested should work.

if you also want to save that changed xml to temp.xml, use:

var fRef:FileReference=new FileReference();

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

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

fRef.save(xml,"temp.xml");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 25, 2013 Apr 25, 2013

It prompts me to save the file from localhost. I don't want a game that consistantly prompts you everytime the game saves.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 25, 2013 Apr 25, 2013

are you saving the xml file onto a server?

if yes, use the urlloader class and call a server-side script that saves your file.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 26, 2013 Apr 26, 2013

I have it on localhost via WAMP, so it'd be saving to 127.0.0.1/xmlDoc.xml

But I don't have a server sided script for it. I'd assume it should be done in php

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2013 Apr 26, 2013

yes, most servers support php.

if you post your xml variable as xmlVar, you can use:

<?php

foreach ($_POST as $key => $value) {

   $$key = $value;

}

$file = "temp.xml";

$fh = fopen($file, 'w') or die("can't open file");

fwrite($fh, $xmlVar);

fclose($fh);

?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 26, 2013 Apr 26, 2013
LATEST

I found a way to load via php to MySQL, which seems to work perfectly. I just need to figure out how to save it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines