Hi,

just posting my code as requested by zentara

cheers

johnk

#!/usr/bin/perl -wT
#
#

use strict;
use XML::DOM;
use CGI qw/:standard -nosticky/;

my $q = new CGI;
my $minMass      = $q->param( "minmass" )     || error( $q, "No Min Mass entered." );
my $maxMass  = $q->param( "maxmass" ) || error( $q, "No Max Mass entered." );

if ($maxMass <= $minMass){
        error ( $q, "Invalid Mass Window entered." );
}


my @tableData;

my $parser = new XML::DOM::Parser;

my $doc = $parser->parsefile("particles.xml");

my @particles = $doc->getElementsByTagName("Particle");

foreach my $particle (@particles){
    my @melement = $particle->getElementsByTagName("Mass");
    my @qualities = $melement[0]->getChildNodes;
    foreach my $quality (@qualities){
        my $thisMass = $quality->toString if ($quality->getNodeType eq TEXT_NODE);
        if ($thisMass >= $minMass && $thisMass <= $maxMass){
            &writeout($particle);
        }
    }
}


sub writeout{
    my $particle = shift;
    my $outName = &getValue($particle,"Name");
    my $outMass = &getValue($particle,"Mass");
    my $outWidth = &getValue($particle,"Width");
    my $outCharges = &getValue($particle,"Charges");
    push @tableData,[$outName,$outMass,$outWidth,$outCharges];
}

sub getValue{
    my ($particle,$tag) = @_;
    my @elements = $particle->getElementsByTagName($tag);
    if ([EMAIL PROTECTED]){return "N/A"};
    my @qualities = $elements[0]->getChildNodes;
    foreach my $quality (@qualities){
        my $thisValue = $quality->toString if ($quality->getNodeType eq TEXT_NODE);
        return $thisValue;
    }
}

my $len = scalar(@tableData);
print $q->header(-type=>'text/html',
                 -expires=>'now');
print $q->header("Pragma: no-cache");
print "<HTML>\n";

if ($len == 0 ) {
    error( $q, "No particles found in mass window." );
    }else{
        print "<TABLE ALIGN=CENTER WIDTH=\"80\%\" CELLSPACING=2 
CELLPADDING=2 BORDER=2 BGCOLOR=LIGHTBLUE>\n";
        print "<TR>\n";
        print "<TD>Name</TD>\n";
        print "<TD>Mass</TD>\n";
        print "<TD>Width</TD>\n";
        print "<TD>Charges</TD>\n";
        for (my $i=0; $i<$len; $i++){
            print "<TR>\n";
            print "<TD>$tableData[$i]->[0]</TD>\n";
            print "<TD>$tableData[$i]->[1]</TD>\n";
            print "<TD>$tableData[$i]->[2]</TD>\n";
            print "<TD>$tableData[$i]->[3]</TD>\n";
            print "</TR>\n";
        }
        print "<TABLE>\n";
    }
print "</HTML>\n";
$doc->dispose;
$q->delete_all;
exit;

sub error {
    my( $q, $reason ) = @_;

    print $q->header(-type=>'text/html',
                 -expires=>'now'),
          $q->start_html( "Error" ),
          $q->h1( "Error" ),
          $q->p( "Your request was not procesed because the following error ",
                 "occured: " ),
          $q->p( $q->i( $reason ) );
          print "$minMass : $maxMass\n";
          $q->end_html;
    exit;
}




On Sat, 17 Apr 2004, zentara wrote:

> On Fri, 16 Apr 2004 12:31:34 +0100 (GMT), [EMAIL PROTECTED] (John
> Kennedy) wrote:
> 
> >Hi,
> >
> >I am a bit of a beginner and this is my first time using a mailing list so
> >please go easy with me.
> >
> >I have a problem with a perl/cgi script I have written.
> >
> >It's a simple script that takes 2 values and parses an xml file for all
> >entries which lie between the two. It then takes the answers and posts a
> >table back so you can see what lies within your specified window.
> >
> >I use
> >
> >use strict;
> >use XML::DOM;
> >use CGI qw/:standard -nosticky/;
> >
> >The script works fine from the command line when debugging and also works
> >fine when I test it from my browser.
> >
> >The problem arrises when I try a second set of values. The script seems to
> >remember the original values and sticks with them BUT the parse of the xml
> >file is messed up. Basically if I submit a second request the error which
> >I output tells me that no entries were found and details the window to be
> >the previous window not the one defined by the new values I use..
> 
> Are you using modperl and are unaware of it?  modperl will keep
> values around.
> 
> >I am really confused about this.
> >
> >To make things worse if I wait a little and click reload with the error
> >still present on my browser window then it correctly uses the new values
> >and correctly parses the xml file..
> >
> >The problem seems time dependent. If I wait for 5 seconds after I submit a
> >request and then submit a new one I get the correct behaviour.(sometimes
> >this takes longer to work).
> >
> >The cgi script seems to be saving the final state of my previous request
> >and I don't know how to stop this..
> >
> >can anybody help??
> 
> You should post the code so people can look at it.
> Some wild ideas which pop into mind.
> 
> -- a weird flocking problem with the XML file ?
> -- some sort of cacheing problem keeping temp files laying around ?
> 
> 
> 
> 
> 

-- 
Dr. John A Kennedy                email: [EMAIL PROTECTED]
LMU Muenchen
Sektion Physik
Am Coulombwall 1                  Phone:  0049(89)2891 4152
D 85748 Garching, Germany         Fax:    0049(89)2891 4103



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to