Wednesday, June 21, 2006

saving XML from Flash, using PHP

I was trying to create a XML file on the server, using PHP. The XML string was being sent from Flash, by adding the same as a property to a LoadVars obj. ( Since I had to send few other data items as well - other than the XML string, I couldn't use Flash's XML.sendAndLoad method; Ok, I can see you guys yelling out, "why not AMFPHP!", but then the app was designed by non Flash clients and they didn't know AMFPHP then)

On the PHP end the following was the code to save the doc.

$doc = new DomDocument();
$doc->loadXML($xmlstr);
$doc->save($picture_path);

When I tried saying the xml, I wasn't able to get it saved. The error was at the loadXML method call. PHP wasnt able to load the XML, from the XMLString. But I was able to trace the XML string. Except for the escape characters for double Quotes ('"' were '\"' at the php end) for the XML node attributes, things looked fine. I tried copying the same string and hardcoding it on the php page, and the XML file was being created, with no issues.

After a few attempts, I was able to narrow down the problem to the attributes in the XMLNodes. the escape characters were indeed the problem. ( am not a php programmer, so, i dont know why)

Now this is what I have as a solution to this:

in the php end, i have done a stripslashes call, before i load it on the DomDocument Object. Like this:

$doc = new DomDocument();
$xmlstr = stripSlashes($xmlstr);
$doc->loadXML($xmlstr);
$doc->save($picture_path);

And it works!
Post a Comment