25d4 Perl and XML - Perl Programming
Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Perl and XML

Posted by ShaSha (ShaSha), 20 September 2005
Hi ! Need help as I am very new to Perl.
Current program only allows user to send 4 fixed variables which will then be processed by Perl. Java code is as follows:

       post.addParameter("Sender","Test");
       post.addParameter("Receiver","Myself");
       post.addParameter("TrackId","010");
       post.addParameter("XMLMessage","<MessageFunction>Testing purpose only</MessageFunction>");

The current Perl program that do the processing is as follows:

use Time::localtime;

&ReadParse;

$logfile = "/myroot/log/mylog.txt";
$allowed_ip = "";
$remote_ip = $ENV{'REMOTE_IP'};
##$targetDirectory = "http://111.111.111.111/test/recv";
$targetDirectory = "/myroot/test/recv";
$backupDirectory = "/main/BACKUP";

$Sender = $in{'Sender'};
$Receiver = $in{'Receiver'};
$TrackingId = $in{'TrackId'};
$Message = $in{'XMLMessage'};


$targetFile = $targetDirectory . "/$Sender-" . $TrackId . ".xml";
$backupFile = $backupDirectory . "/$Sender-" . $TrackId . ".xml";

## $targetInfo = $targetDirectory . "/$Sender-" . $TrackId . ".xrf";

$timestamp = ctime();

open(LOGF, ">>$logfile") || die "\nError opening $logfile\n";
printf LOGF "\n************************************************\n";
printf LOGF "Sender                  :      $Sender\n";
printf LOGF "Receiver                  :      $Receiver\n";
printf LOGF "Track ID                  :      $TrackId\n";
printf LOGF "TimeStamp                :        $timestamp\n";
printf LOGF "Main Directory Filename      :      $targetFile\n";
close(LOGF);

##open(INFFILE, ">$targetInfo") || die "\nError opening $targetInfo\n";
##print INFFILE "$Sender\n";
##print INFFILE "$Receiver\n";
##print INFFILE "$TrackId\n";
##print INFFILE "$targetFile\n";
##close(INFFILE);

open(EDIFILE, ">$targetFile") || die "\nError opening $targetFile\n";
print EDIFILE "$Message";
close(EDIFILE);

open(EDIFILE, ">$backupFile") || die "\nError opening $backupFile\n";
print EDIFILE "$Message";
close(EDIFILE);

print "Content-Type: text/html", "\n\n";
print "Message number : $TrackId has been submitted to TEST";

1;


sub ReadParse {
 local (*in) = @_ if @_;
 local ($i, $key, $val);

 if ( $ENV{'REQUEST_METHOD'} eq "GET" ) {
   $in = $ENV{'QUERY_STRING'};
 } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
          read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
   }
   else {
       # Supply name/value form data as a command line argument
       # Format: name1=value1\&name2=value2\&...
       # (need to escape & for shell)
       # Find the first argument that's not a switch (-)
       $in = ( grep( !/^-/, @ARGV )) [0];
       $in =~ s/\\&/&/g;
   }

                             
   @in = split(/&/,$in);

BUt now need to post XML file to the server. Code in Java....

     String sURL = "http://111.111.111.111/cgi-bin/outbnd.pl";
         String sXMLFilename = "c:\\Testing.xml";
     PostMethod post = new PostMethod(sURL);
     post.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(input), input.length()));
     post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
     HttpClient httpclient = new HttpClient();

How can the current Perl program cater to this? Can it read all the XML tags in the Testing.xml? Me totally
at a wits end now as this task is urgent but this is my first time in Perl. Please advice. Thanks in advance.

Posted by admin (Graham Ellis), 20 September 2005
It looks to me like the XML data exists already - you just want to read it from one source and print it to another.  Have I read that right?

If so ...

open (FH,"input.xml");
read (FH,$buffer,-s "input.xml");
print "Content-type: text/xml\n\n":
print $buffer;





This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.
fb65

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho
0