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
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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("XMLMessage","<MessageFunction>Testing purpose only</MessageFunction>");

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

use Time::localtime;


$logfile = "/myroot/log/mylog.txt";
$allowed_ip = "";
$remote_ip = $ENV{'REMOTE_IP'};
##$targetDirectory = "";
$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";

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

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

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

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


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") {
   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 = "";
         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 and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho