Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

XML-Parser with web application

by sanku (Beadle)
on Jul 26, 2010 at 11:24 UTC ( [id://851356]=perlquestion: print w/replies, xml ) Need Help??

sanku has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I need some guidance in my program. I have created a web application tool using XML::XSLT, XML::Smiple and XML::Parser and creating XML out file is working fine but there is a delay in writing the file can any one give me some best idea to rectify it. Thanks in advance. Program to create XML files.

#!/perl/bin/perl print "Content-type:text/html\n\n"; use CGI qw(:standard); use strict; use warnings; use XML::Simple; use XML::XSLT; use Data::Dumper; use Data::FormValidator; use Data::FormValidator::Constraints qw(email); use Net::SMTP; my $tech = { 'Linux' => 'linux@domain.com', 'Unix' => 'unix@domain.com', 'Windows XP' => 'windows@domain.com', }; # ----------------- Sub routines ----------------- sub showfun1 { my $xml->{items} = { item => [ sort keys %{$tech} ] }; $xml->{rootname} = 'data'; $xml->{nextpage} = 'frmform'; return $xml; } sub write_xml { my ($ref, $fn) = @_; my $root_name = $ref->{rootname}; my $xmld = new XML::Simple( rootname => $root_name, noattr => 1 ); my $data = $xmld->XMLout( $ref ); open(OUT , ">$fn") or die $!; print OUT "$data"; close (OUT); } sub showfun2 { my ($name) = @_; my $xml = showfun1(); $xml->{name} = param("$name"); $xml->{mail} = $tech->{$xml->{name}}; $xml->{nextpage} = 'frmconfirm'; return $xml; } sub showfun3 { my $xml = showfun2("tt"); # Input my $input; $input->{"$_"} = param("$_") foreach ( param() ); # Profile my $profile = { required=> [qw(sendermail mob username designation)], constraint_methods => { sendermail => email(), username => qr/^([A-Z|a-z]{6,15})$/, mob => qr/^([0-9]{10})$/, }, }; # Validation my ($valids, $missings, $invalids, $unknowns) = Data::FormValidato +r->validate($input, $profile); print "Missing :", scalar(@$missings)."<br />"; print "Invalid :", scalar(@$invalids)."<br />"; if ( (scalar(@$missings)) ) { $xml->{missing} = $missings; } if ( (scalar(@$invalids)) ) { $xml->{invalid} = $invalids; } if ( (scalar(@$missings)) ==0 && (scalar(@$invalids)) == 0){ my $server='IP'; my ($mailfrom, my $mailto ) = @_; my $subject = 'Test mail from perl program'; my $message = 'Hi this is a test mail which is generated by pe +rl program'; my $smtp = Net::SMTP->new($server, Timeout => 15, Debug => 1); $smtp->mail ( $mailfrom ); $smtp->to ( $mailto ); $smtp->data(); # Start the mail # headers $smtp->datasend ("From: $mailfrom\n"); $smtp->datasend ("To: $mailto\n"); $smtp->datasend ("Subject: $subject\n"); $smtp->datasend ("\n"); # body. $smtp->datasend ("$message\n\n"); my $ss=$smtp->dataend(); # Send the termination string # Construct final XML if($ss != 0) { $xml->{uname} = param('username'); } if($ss == 0) { $xml->{errorcode} = '530 5.7.1 Client was not authenticat +ed'; } $xml->{rootname} = 'confirm'; } return $xml; } # ----------------- Main Programme ----------------- # Var Declarations my $fname = param('nextpage'); my $ret; my $xml_file = "abc.xml"; my $xsl_file = "abc.xsl"; # Validation if ( $fname eq 'frmform' ) { $ret = showfun2("selopt"); } elsif ( $fname eq 'frmconfirm' ) { $ret = showfun3(); }else { $ret = showfun1(); } # Writing into XML write_xml($ret,"$xml_file"); # Convertion from XML to HTML my $p = XML::XSLT->new ($xsl_file, "FILE"); $p->transform_document ($xml_file, "FILE"); $p->print_result();

XSLT code

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" versi +on="1.0"> <xsl:output method="html" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <link rel="stylesheet" href="css1.css" media="screen,projection" t +ype="text/css" /> <form name="frm" method="post" > <xsl:if test="/data"> <h1><font color='black'><center>Login Page - Forum</center +></font></h1> <table align="center" width='21%'> <tr> <td> Technology Category </td> <td> <select name="selopt" onchange="document.frm.s +ubmit();" style="width: 168px;"> <option>Select </option> <xsl:for-each select="/data/items/item"> <option> <xsl:if test="/data/name = . "> <xsl:attribute name="selected"> selected </xsl:attribute> + </xsl:if> <xsl:value-of select="."/> </option> </xsl:for-each> </select> </td> </tr> </table> </xsl:if> <xsl:if test="/data/name"> <table align='center' > <tr > <td> Selected Category </td> <td> <input type="hidden" name="tt1" disabled="true +" size='25'> <xsl:attribute name="value"> <xsl:value-of select="/data/name"/> </xsl:attribute> </input> <input type="text" name="tt" size='25'> <xsl:attribute name="value"> <xsl:value-of select="/data/name"/> </xsl:attribute> </input> <input type="hidden" name="mailid"> <xsl:attribute name="value"> <xsl:value-of select="/data/mail"/> </xsl:attribute> </input> <input type="hidden" name="tech"> <xsl:attribute name="value"> <xsl:value-of select="/data/name"/> </xsl:attribute> </input> <input type="hidden" name="selopt"> <xsl:attribute name="value"> <xsl:value-of select="/data/name"/> </xsl:attribute> </input> </td> </tr> <tr > <td> Name </td> <td> <input type="text" name="username" value=" +" size='25'></input> <xsl:for-each select="/data/missing"> <xsl:if test=". ='username'"> <span id="un"><font color="red"><i +>Username is Missing</i></font></span> </xsl:if> </xsl:for-each> <xsl:for-each select="/data/invalid"> <xsl:if test=". ='username'"> <span id="un"><font color="red"><i>Use +rname is Invalid</i></font></span> </xsl:if> </xsl:for-each> </td> </tr> <tr > <td> Extn/Mob No </td> <td> <input type="text" name="mob" size='25'></inpu +t> <xsl:for-each select="/data/missing"> <xsl:if test=". ='mob'"> <span id="mo"><font color="red"><i +>Mobile No is Missing</i></font></span> </xsl:if> </xsl:for-each> <xsl:for-each select="/data/invalid"> <xsl:if test=". ='mob'"> <span id="mo"><font color="red"><i>M +obile No is Invalid</i></font></span> </xsl:if> </xsl:for-each> </td> </tr> <tr > <td> E-mail </td> <td> <input type="text" name="sendermail" size +='25'></input> <xsl:for-each select="/data/missing"> <xsl:if test=". ='sendermail'"> <span id="em"><font color="red"><i>M +ail-id is Missing</i></font></span> </xsl:if> </xsl:for-each> <xsl:for-each select="/data/invalid"> <xsl:if test=". ='sendermail'"> <span id="em"><font color="red"><i>Mai +l-id is Invalid</i></font></span> </xsl:if> </xsl:for-each> </td> </tr> <tr > <td> Description </td> <td> <textarea name='designation' rows='3' cols='19 +'> <xsl:text> </xsl:text> </textarea> </td> </tr> <tr > <td colspan="2" align='right'> <input type='Reset' name='Reset' value='Res +et'></input> <xsl:text> </xsl:text> <input type="Button" name='Submit' value="Subm +it" onclick="submit();"></input> </td> </tr> </table> </xsl:if> <xsl:if test="/confirm"> <table align="Center"> <xsl:if test="/confirm/uname"> <tr> <td>User Name : <xsl:value-of select="/ +confirm/uname"/></td> </tr> <tr> <td>Selected Category : <xsl:value-of +select="/confirm/name"/></td> </tr> <tr> <td>Sender Mailid : <xsl:value-of select="/c +onfirm/mail"/></td> </tr> </xsl:if> <xsl:if test="/confirm/errorcode"> <tr> <td><xsl:value-of select="/confirm/errorcode +"/></td> </tr> </xsl:if> <tr> <td><a href='http://localhost/frm.pl'>Back</a></ +td> </tr> </table> </xsl:if> </form> </xsl:template> </xsl:stylesheet>

Replies are listed 'Best First'.
Re: XML-Parser with web application
by dHarry (Abbot) on Jul 26, 2010 at 11:58 UTC

    When I run into performance issues/surprises I profile the code to find out what exactly is the bottleneck. You post quite a lot of code. Maybe you should narrow it down a little bit. Where exactly and how much is the delay, have you measured it? As a general remark: if you post some XSLT you should also post the XML it is acting on. Although I don't think the problem is in the XSLT transformation.

    Cheers

    Harry

      Actually it's taking some fraction of time to create XML file. With in writing the file the form is submitting the value. That's my problem. I want to know weather there is any way to submit a form after completing the XML file. Thanks

        I'm not sure if I understand your question correctly. Typically a user submits a form by clicking on a button, the server does some processing and returns the results to the user. Do you mean send the results back to the user? I don't understand the logic of your script. For one thing I would expect a "$var new CGI;" somewhere. But you also send an email with data. I suggest you first write a simple CGI application that works and later add the XML stuff. See CGI Programming for help.

Re: XML-Parser with web application
by Anonymous Monk on Jul 26, 2010 at 11:45 UTC
    but there is a delay in writing the file

    What order of magnitude is the delay?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://851356]
Approved by almut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-26 09:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found