View Full Version : write to .xml file
arun vallappan
07-29-2007, 10:52 PM
does anybody know how to save data into a .xml file...... is it possible to do it locally .... if you are suggesting sendandload to .php file it doesnt work anymore in flex 2 .... so please tell me if there is any method
thanks
selva
drkstr
07-30-2007, 10:33 PM
This is how I post XML to a server side script.
var request:URLRequest = new URLRequest(myGateway);
var form:URLVariables = new URLVariables();
var newPageXML:XML = <navTree></navTree>;
//post XML file to the server
form.fileName = 'navTree.xml';
form.fileContents = newPageXML.toXMLString();
request.method = 'POST';
request.data = form;
//use this for release
sendToURL(request);
//use this for debugging
// navigateToURL(request);
Best regards,
...aaron
arun vallappan
07-30-2007, 11:20 PM
URLVariables class does not have methods filename and filecontents in Flex 2 ...
can you help me out with this ....... and myGateway is just a string is it ... because you generally mention a valid url as the parameter
thanks a lot
drkstr
07-31-2007, 12:31 AM
You are correct, sorry I had that declared in my class code. myGateway is the url that points to the serverside script. Such as "http://localhost/get_xml.php".
URLVariables works like a dynamic object. In the way I use it, 'fileName' and 'fileContent' become the form variables the script reads in on a POST request.
Hope this clears things up a bit,
...aaron
arun vallappan
07-31-2007, 10:48 AM
I am sorry ... i dont get you ....... it would be helpful if you could tell me how to use these methods ... an object of URLVariables cannot access these methods so can you tell me how to use it .... thanks a lot
drkstr
07-31-2007, 06:29 PM
I don't know how else to explain it. Here's an example.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
<mx:Script>
<![CDATA[
import flash.net.*;
private var myXML:XML = <xml><node data="this is data" /></xml>;
private var myGateway:String = 'http://localhost/test/post_xml.php';
private function onClick(): void {
var request:URLRequest = new URLRequest(myGateway);
var form:URLVariables = new URLVariables();
//create form vars dynamicly
form.fileName = 'navTree.xml';
form.fileContents = myXML.toXMLString();
//tell the request object to post form data when it makes request
request.method = 'POST';
request.data = form;
//use for release
//sendToURL(request);
//use for debugging
navigateToURL(request);
}
]]>
</mx:Script>
<mx:Panel title="XML Demo" >
<mx:Button label="Send XML" click="onClick();" />
</mx:Panel>
</mx:Application>
file hosted on /test/post_xml.php
<?php
print "<b>FIle Name: </b>" . $_POST['fileName'] . "<br />";
print "<b>FIle Content: </b>" . urlencode( $_POST['fileContents'] );
?>
The Output being:
FIle Name: navTree.xml
FIle Content: %3Cxml%3E%0A++%3Cnode+data%3D%5C%22this+is+data%5C %22%2F%3E%0A%3C%2Fxml%3E
Hope this helps. Let me know if you have any question about how things work.
Regards,
...aaron
arun vallappan
07-31-2007, 10:48 PM
thanks a lot ....... that really helped ..... and i am sorry for taking a lot of your time
thanks a bunch again
regards
selva
arun vallappan
08-01-2007, 03:27 PM
can you please tell me why it is not saving in the XML format ... it would be helpful if you can tell me where i should make the changes for this to happen.
drkstr
08-01-2007, 09:27 PM
Are you using urlencode( )? I only did this so it would print in an html page. You would want to save the contents of $_POST['fileContents'] directly to the file without using urlencode().
Let me know if you're still having trouble.
Best regards,
...aaron
arun vallappan
08-01-2007, 11:10 PM
thanks ..... but i am getting extra \ at unwanted places .. this is the code that i had used
<?php
if ($_POST) {
$xmlString = $_POST['fileContents'];
$filePath = $_POST['fileName'];
$file = fopen($filePath, "w");
$write = fwrite($file,$xmlString);
fclose($file);
} else {
echo "this script works from Flash only!";
}
?>
and the output i got is this
<xml>
<node data=\"this is data\"/>
</xml>
do you see those extra slashes ... the xml tags are perfect when sent but there is some error with the php file i had written i guess ... can you help me out
regards
drkstr
08-01-2007, 11:51 PM
Ahh, interesting. I actually used Python for my server side script when I needed to post the XML. Since I don't remember this being a problem, it's probably a PHP issue.
Try using:
file_put_contents( 'my_file.xml', stripslashes($_POST['fileContents']) );
That seems to do the trick. Also, if the file name is static, I would hard code to the program so as to mitigate some security risks. Allowing an unauthenticated user to save any content to any file is begging for disaster. ;)
Hope this helped.
Best regards,
...aaron
arun vallappan
08-02-2007, 09:28 AM
yes that helped a lot .... thanks aaron
regards
adove
11-04-2009, 08:19 PM
I use ASP and C# to program and want to accomplish the same thing you are doing in PHP but with ASP and C#. Do you know how? Or could you help point me in the right direction?
Thanks
Alex
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.