Groups | Blog | Home
all groups > flash data integration > october 2005 >

flash data integration : XML to CGI using Perl


jeff28
10/15/2005 12:00:00 AM
Hi there everyone, my name is Jeff and I have a problem.

I need to save an xml file to the server upon creation in flash app. I'm
sending the xml data via xml class xmlVal.send(url) to a cgi script that would
handle the saving/creation of file on server. I think my problem is in the cgi
script, not sure though. This is the cgi script written in Perl, using 'XML-DOM
mod':

just kidding, apparently I needed to attach it via Attach Code or else it was
loaded with emoticons!

Very simple, I just need to save the file, but it's not working. I know it's
not a problem regarding permissions, I've tested opening/saving xml files by
calling cgi scripts in browsers and that all works. I feel like my problem is
that I'm not reading the sent data correctly in cgi script. Any thoughts,
because I've burn't all my thoughts to carbon bits, so any help would be beyond
appreciation! Thank you so much for your time!

I've attached the Perl code to this message.

Jeff


#!/usr/bin/perl

use XML::DOM;

my $parser = new XML::DOM::Parser;
$input = <STDIN>;
my $doc = $parser->parsefile ($input);
$doc->printToFile ("test30.xml");
jeff28
10/18/2005 1:11:57 PM
Hey everyone! I worked it out with Perl saving my files and dynamically
creating filenames from the xml data. The problem was I was parsing the xml
string when I didn't need to do that at all. Simply listened for the xml
through <STDIN>, then used simple filehanding functions via basic Perl. I ended
up having to use XML-DOM for parsing to pull out filenames to save to. Works
great! Thanks for reading, if anyone is interested, I can post the code I used
upon request.
Take Care everyone!

Jeff
EduardoBM
11/1/2005 12:00:00 AM
could you post the entire code ?? i think i had the same problem...

thunx

jeff28
11/1/2005 12:00:00 AM
Howdy EduardoBM, I have attached the code you requested. I ended up using
XML::MOD to parse the xml string sent and use an attribute as a filename for
the new file. I chose XML-DOM because I'm most familiar with it, I'm sure there
are roughly five thousand other mods to suit your needs.

In the Perl, I save the file using temp xml filename just to grab the data
sent from flash, I couldn't do this with XML-MOD. The rest of the code lightly
demonstrates using this mod to load the xml file, parse it, reading an
attribute, and then saving it to a new file name or the same file name,
whatever the attribute reads in the xml file. The final print statement sends
the data back to flash to confirm the cgi file was actually found.

The flash code only shows how to load the xml data into flash, and then once
loaded, send it to the cgi script to save the file on the server. Of course, as
I'm sure you realize, this is completely useless without actually modiftying
the xml in flash somehow first before sending it to be saved. Thankfully flash
comes equipped with a glorious xml class so that you can have your flash app
first load the xml, then before using the sendAndLoad(), modify the xml data,
and then send it to wherever you want, in this case, a cgi file.

Lastly, Perl is in its most skeletal existence in this example. More
practically, particularly using XML-MOD, you can easily traverse the xml string
sent from flash to make any neccessary changes you see fit. i.e., modify the
xml string into well formed xml data for multiple device/app deployment and
legibility. Also, the xml data sent back to flash could verify your new xml
data and that it is actually saved by using basic I/O Perl functions to open
the new file, and using the print command, send that string back to flash,
instead of the original.

Well, I wish you the best of luck, and I hope you find the answer you are
looking for, if not here, somewhere! And if you come across any great practices
or tips related, I encourage you to post them! I know I almost never find the
answer I'm looking for in the forums, maybe it's because I just suck at using
them. Whatever the case, take care.

Jeff



Perl code:

#!/usr/bin/perl

use XML::DOM;

my $input = join(' ',<STDIN>);

open (FILEHANDLE, ">tempxml.xml");
print FILEHANDLE $input;
close FILEHANDLE;
my $parser = new XML::DOM::Parser;
my $data = $parser->parsefile ("tempxml.xml");
my $documentElementNode = $data->getDocumentElement();
my $textNode = $documentElementNode->getFirstChild();
my $calendarNode = $textNode->getNextSibling();
my $xmlFile = $calendarNode->getAttribute ("name");
$data->printToFile($xmlFile);
unlink ("tempxml.xml");
print "Content-type: text/xml\n\n";
print "$input\n";


Actionscript 2.0 code:

//the xmlLoader function is called to handle the returned xml data from the
cgi file
var xmlLoader:XML = new XML();
xmlLoader.onLoad = function(bsuccess:Boolean):Void {
if (bsuccess) {
cgiTester.text = xmlLoader.toString();
} else {
cgiTester.text = xmlLoader.toString();
}
};
var xmlVal:XML = new XML();
xmlVal.ignoreWhite = true;
xmlVal.load("xmlFiles/test50.xml");
xmlVal.onLoad = function(success:Boolean):Void {
if (success) {
trace(xmlVal);
xmlVal.addRequestHeader("Content-Type", "text/xml");
xmlVal.sendAndLoad("cgi-bin/test50.cgi", xmlLoader);
}
};
EduardoBM
11/1/2005 12:00:00 AM
thank you very much!!

AddThis Social Bookmark Button