Results 1 to 4 of 4
Thread: problem saving xml to server
-
July 3rd, 2012, 02:39 AM #126Registered User
postsproblem saving xml to server
I'm having a small problem here. I am running a localhost Apache PHP server. What I am trying to do is load an xml file, modify it, and send it back to the server with PHP.
I am able to load the xml file and modify it but when I save back to the server the xml file is empty or null.
Here is the as code:
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest('test.xml'));
function processXML(event:Event):void {
myXML=new XML(myLoader.data);
}
var xmlcontents:String=myXML;
var foldername:String="testfolder";
var filename:String="test.xml";
var dataPass:URLVariables = new URLVariables();
var urlLoader:URLLoader = new URLLoader();
var previewRequest:URLRequest=new URLRequest("http://localhost/jd/saving-xml.php");
previewRequest.method=URLRequestMethod.POST;
dataPass.filename=filename;
dataPass.xmlcontents=xmlcontents;
dataPass.foldername=foldername;
previewRequest.data=dataPass;
urlLoader.load(previewRequest);
When I do this in the as code it works fine:
var myXML:XML=
<people>
<item>
<name>George</name>
</item>
<item>
<name>Bill</name>
</item>
<item>
<name>Henry</name>
</item>
</people>;
It's only when I load the xml document externally I am getting a blank xml document when I save the xml back to the server.
The PHP code )I think this part is fine):
<?php
// POST variable
$fileName = $_POST["filename"];
// POST variable
$xmlContents = $_POST["xmlcontents"];
// backslashes from xml string (skip this for plain text)
$lastBackslashPos = strpos ($xmlContents, "\\");
while($lastBackslashPos >0){
$xmlContents = substr($xmlContents,0,$lastBackslashPos)
.substr($xmlContents,$lastBackslashPos+1,strlen($x mlContents));
$lastBackslashPos = strpos ($xmlContents, "\\");
}
// POST foldername
$foldername = $_POST["foldername"];
// test if the folder name is existing in the server (skip this if you want to test in server path)
if(!is_dir($foldername . "/")) {
mkdir($foldername . "/", 0755);
}
// write xml data to file on server relatively to server path of the this PHP file
$fh = fopen($foldername . "/" . $fileName, "w");
fwrite($fh, $xmlContents);
fclose($fh);
?>
Thanks in advance.
-
July 3rd, 2012, 02:50 AM #2
A couple of suggestions
- Have you tried to run the PHP script from an HTML form? This would help you to understand if it really works well
- Try to register an Event.COMPLETE listener to your loader, and within that trace the following:
If the PHP script throws an error, a warning, or an exception, with this method you will be able to see it and understand where the error is.Code:urlLoader.addEventListener(Event.COMPLETE, scriptDone, false, 0, true); function scriptDone(evt:Event):void{ trace(evt.target.data); }
-
July 3rd, 2012, 02:10 PM #326Registered User
posts
Thanks. I tried this but I'm not getting any errors. It writes a xml file to the server which is what I want. But when I open it up I get a empty document which says null.
It works fine when I paste the xml code directly into the as panel like this (instead of loading it in an external xml file):
var myXML:XML=
<people>
<item>
<name>George</name>
</item>
<item>
<name>Bill</name>
</item>
<item>
<name>Henry</name>
</item>
</people>;
I am able to open the xml from the server and everything is there. Which leads me to think the php part is fine but it is not reading the loaded xml external file correctly.
-
July 4th, 2012, 05:59 AM #4
Take a look here. With this tutorial, I'm pretty sure you'll quickly get to your goal: http://www.kirupa.com/net/writingXML_pg1.htm

Reply With Quote


Bookmarks