Writing XML data to a user provided file
I am a total newbie to Perl and am attempting to write a script which will uses cgi-lib and will take XML formatted input (in a simple scrolling form) and write it to a user specified file. I have made an effort with
http://205.178.180.102/~businesk/sapdirectory/xmlupld.html -however we are currently having permission problems. Do you need to use an FQDN to use cgi?
Basically I figured out I need to use
the &readparse routine to save the content as a file, however this seems to be easier said than done!
Note: We have been recommended to use CGI.pm and are in the process of installing it so help for that would be appreciated too.
Also if you need access to ftp / SSH, email me and I will give it to you.
Dave
Ken Elliott posted this at 13:06 — 19th October 2000.
They have: 358 posts
Joined: Jun 1999
Unfortunately I haven't much experience at XML..to be precise .. just about NULL experience. However if I am grasping the concept of your problem, you just want to write the XML text to a file. If that is so, this little code would do exactly what I think you want.
############## COPY CODE BELOW #################
#!/usr/bin/perl
&Parse_Form; # puts the input in a hash
$file = $formdata{'xmlfn'} # user specified file
$xmldata = $formdata{'xmldata'}; # inputed xml data
# open text file change the extension if you don't want txt
open (FILE, "> ../$file.txt") || &error('file open error');
flock(FILE, 2); # lock the file
print FILE "$xmldata"; # print the xmldata to file
flock(FILE, 8); # unlock the file
close (FILE); # close the file
print "Content-type:text/html\n\n"; # mime type
print "Done"; # so it won't error with no content
exit;
# Parser subroutine : use either post or get
sub Parse_Form {
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
if ($ENV{'QUERY_STRING'}) {
@getpairs =split(/&/, $ENV{'QUERY_STRING'});
push(@pairs,@getpairs);
}
} else {
print "Content-type: text/html\n\n";
print "Use Post or Get";
}
foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /;
$key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~s///g;
if ($formdata{$key}) {
$formdata{$key} .= ", $value";
} else {
$formdata{$key} = $value;
}
}
}
# Error Subroutine
sub error {
print "SORRY THERE WAS AN ERROR:$_[0];";
}
######## END OF SCRIPT ########
That little tidbit will take a user specified file from your form, and write the xml data to it. Make sure the permission are set correctly...755 for scripts...777 for text files.
And don't forget to add a submit button to your form.
Now may I ask for a favor? It appears that you have a grasp on XML...something I haven't had time to accomplish. Do you think you can brief me on some of XML's aspects? Perhaps a general tutoring session? Thanx.
Hope that helps you out.
[Edited by Ken Elliott on 10-19-2000 at 09:09 AM]
Pimpin like a pimp with an electrofied pimpin machine!
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.