Skip to main content
Inspiring
May 13, 2013
Question

read xml content before saving to it

  • May 13, 2013
  • 1 reply
  • 1051 views

Actionscript:

var txtFld:TextField = new TextField();

txtFld.width = 500;

txtFld.height = 350;

txtFld.multiline = txtFld.wordWrap = true;

addChild(txtFld);

var str:String = "<user>1";

str +=  "<value>Sent from ActionScript</value>";

str +=  "<value>Sent from ActionScript</value>";

str +=  "<value>Sent from ActionScript</value>";

str +=  "</user>";

var xmlToSend:XML = new XML(str);

var req:URLRequest = new URLRequest("save_xml.php");

req.data = xmlToSend;

req.contentType = "text/xml";

req.method = URLRequestMethod.POST;

var urlLoader:URLLoader = new URLLoader();

urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);

urlLoader.addEventListener(Event.COMPLETE, onComplete, false, 0, true);

urlLoader.load(req);

function onIOError(evt:IOErrorEvent):void

{

          txtFld.text = "XML load error.\n" + evt.text;

}

function onComplete(evt:Event):void

{

          try

          {

                    urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);

                    urlLoader.removeEventListener(Event.COMPLETE, onComplete);

                    var loadedXML:XML = new XML(evt.target.data);

                    txtFld.text = loadedXML;

          }

          catch (err:Error)

          {

                    txtFld.text = "XML parse error:\n" + err.message;

          }

}

PHP:

<?php

  if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){

          $data = $GLOBALS["HTTP_RAW_POST_DATA"];

          $file = fopen("data.xml","wb");

          fwrite($file, $data);

          fclose($file);

}

?>

This actionscript/php basically creates the data.xml file if it doesn't exist yet and adds the content of the 'str' string to it. The 'user' tag with some 'value' tags between them. In this case user nr 1. The script opens the data.xml file (or creates it the 1st time), writes the xml data in it received from Flash and closes the file.

That's not entirely how I want to make it work. I want it to open the data.xml file, then check how many 'user' tags there are at that moment. Then it should add the 'str' string to it, but with the 'user' tag having a value of the number of 'user' tags currently in the data.xml file plus one. So if the script reads the data.xml file first and sees that currently there are 3 user tags in it, the content added to it should be:

var str:String = "<user>4";

str +=  "<value>Sent from ActionScript</value>";

str +=  "<value>Sent from ActionScript</value>";

str +=  "<value>Sent from ActionScript</value>";

str +=  "</user>";

How can I do this? I'm guessing the php script should first return the content of the data.xml file. So Flash can get the number of user tags back so it can put together the correct str string with 1 added to the number of users. And then send that string back to php in order to write and close the file?

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
May 13, 2013

first read your xml file and return its contents to flash then edit your xml file in flash and then write it.

to read it, use a different php file (or the same but send something to indicate a read only) and to edit it use something like:

function editXML(xml:XML):void{

var num:int = int(xml.@num);

xml.@num=num+1;

xml.appendChild(<value>Sent from ActionScript</value>);

saveXML(xml);  // use a different php file to write your xml or use the same by send somthing to indicate the php should write.

}

jiggy1965Author
Inspiring
May 13, 2013

When I load data.xml in Flash through an URLrequest and edit its content in Flash (adding 1 to the 'user' tags etc.), in the mean time someone else could do that too. He or she could be finished already by the time I'm saving my xml content back. As a result the number of 'user' tags and content would be incorrect and his/her file overwritten.

That's probably why you're suggesting PHP to load data.xml instead of an URLRequest? So that the data.xml file remains 'open' and can't be written to?

<?php

          $file = fopen("data.xml","r");

         

          echo $contentoffile

}

?>

How do I echo the content of data.xml back to Flash so I can process it further there (reading the number of 'user' tags, appendChild the next user and content, calling the savePHP function)?

kglad
Community Expert
Community Expert
May 13, 2013

to echo file contents use:

<?php

          $file = fopen("data.xml","r");

$contents=fread($file,filesize($file));

fclose($file);

echo $contents;

}

?>

p.s. you should have your php update the user number if you expect multiple users.

p.s.s.  in editXML() i assumed you changed your xml structure to the better:

<user num="5">

  <value>Sent from ActionScript</value>

  <value>Sent from ActionScript</value>

  <value>Sent from ActionScript</value>

  <value>Sent from ActionScript</value>

</user>