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

read xml content before saving to it

Contributor ,
May 13, 2013 May 13, 2013

Copy link to clipboard

Copied

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?

TOPICS
ActionScript

Views

808

Translate

Translate

Report

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 ,
May 13, 2013 May 13, 2013

Copy link to clipboard

Copied

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.

}

Votes

Translate

Translate

Report

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
Contributor ,
May 13, 2013 May 13, 2013

Copy link to clipboard

Copied

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)?

Votes

Translate

Translate

Report

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 ,
May 13, 2013 May 13, 2013

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

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
Contributor ,
May 13, 2013 May 13, 2013

Copy link to clipboard

Copied

I've managed to load the xml through PHP, read the amount of users and add one to it including some of that user's value tags.

var selectedMovies:Array = new Array();

//

for (var p:int=0; p<selectedArray.length; p++)

{

          var clipOrigin:Array = indexOf2D(clipArray,selectedArray

);

          var quoteFilm:String = personageQuotes_XML.personage[which[clipOrigin[0]]].fragment.film[clipOrigin[1]];

          selectedMovies

= quoteFilm;

}

//

var reqLoad:URLRequest = new URLRequest("load_xml.php");

var urlLoader:URLLoader = new URLLoader();

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

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

urlLoader.load(reqLoad);

function onIOErrorLoader(evt:IOErrorEvent):void

{

          trace("XML load error.\n" + evt.text);

}

function onCompleteLoader(evt:Event):void

{

          urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onIOErrorLoader);

          urlLoader.removeEventListener(Event.COMPLETE, onCompleteLoader);

          try

          {

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

                    var newUser:int = loadedXML.user.length() + 1;

                    var str:String = "<user num=\"" + newUser + "\">\r";

                    //

                    for (var s:int=0; s<selectedMovies.length; s++)

                    {

                              str +=  "<value>" + selectedMovies + "</value>\r";

                    }

                    //

                    str +=  "</user>\r";

                    loadedXML.appendChild(str);

                    myText.text = loadedXML;

                    //

                    var xmlToSend:XML = loadedXML;

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

                    reqSender.data = xmlToSend;

                    reqSender.contentType = "text/xml";

                    reqSender.method = URLRequestMethod.POST;

                    var urlSender:URLLoader = new URLLoader();

                    urlSender.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorSend, false, 0, true);

                    urlSender.addEventListener(Event.COMPLETE, onCompleteSend, false, 0, true);

                    urlSender.load(reqSender);

                    function onIOErrorSend(evt:IOErrorEvent):void

                    {

                              trace("XML load error.\n" + evt.text);

                    }

                    function onCompleteSend(evt:Event):void

                    {

                              try

                              {

                                        urlSender.removeEventListener(IOErrorEvent.IO_ERROR, onIOErrorSend);

                                        urlSender.removeEventListener(Event.COMPLETE, onCompleteSend);

                              }

                              catch (err:Error)

                              {

                                        trace("XML parse error:\n" + err.message);

                              }

                    }

          }

          catch (err:Error)

          {

                    trace("XML parse error:\n" + err.message);

          }

}

//

The first entry goes well, but something strange happen on further entries. The greater-than and smaller-than signs are replaced with &gt; and &lt;

<user num="1">

  <value>clip02.flv</value>

  <value>clip01.flv</value>

  <value>&lt;user num="1"&gt;

&lt;value&gt;clip01.flv&lt;/value&gt;

&lt;value&gt;clip02.flv&lt;/value&gt;

&lt;/user&gt;</value>

</user>

Don't know why this happens but I would rather have above xml content as:

<user num="1">

  <value>clip02.flv</value>

  <value>clip01.flv</value>

  <value>

<user num="2">

<value>clip01.flv</value>

<value>clip02.flv</value>

</user></value>

</user>

For some reasons those characters are replaced with their html identities. Also, the number of the user isn't added by 1. But that's probably because of that conversion of < and > which makes the xml not readable correctly

Somewhere there's an error in my code but I can't find it?

Votes

Translate

Translate

Report

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 ,
May 13, 2013 May 13, 2013

Copy link to clipboard

Copied

LATEST

you can apply html_entity_decode() to your string before saving it.

Votes

Translate

Translate

Report

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