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

Trying to parse data from client to server and the inserting into database. case 1) The same worked when i tried to parse and insert directly by giving file name i.e nba.xml instead of variable $xml case 2)The same code worked when i only tried to parse by giving variable $xml in parse file..and not insert.. i.e parsing was successful But it gives me below error when i tried to insert the same after parsing Error message: HTTP POST error code: 500 HTTP POST error message: Internal Server Error
customer.xml <?xml version="1.0"?> <customer> <first_name>Frank</first_name> <last_name>Sanbeans</last_name> <dob>3/10</dob> <email>frank@example.com</email> </customer>
nba.xml <?xml version="1.0"?> <stats><player><name>Houston, Allan</name><g>69</g><ppg>20.1</ppg><rpg +>3.4</rpg><apg>2.8</apg><blk>14</blk></player> <player><name>Sprewell, Latrell</name><g>69</g><ppg>19.2</ppg><rpg>4.5 +</rpg><apg>4.0</apg><blk>15</blk></player> <player><name>Ewing, Patrick</name><g>49</g><ppg>14.6</ppg><rpg>10.0</ +rpg><apg>1.0</apg><blk>68</blk></player> </stats>
supplier.xml <?xml version="1.0"?> <supplier-data> <supplier> <name>Supplier 1</name> <address>Address 1</address> <tel-no>012345</tel-no> </supplier> </supplier-data>
twig_client_parse #!/usr/bin/perl
use strict; use warnings; use XML::Simple; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $server_endpoint = "http://localhost/cgi-bin/RnD/twig/twig_rcvr_ser +ver.cgi"; # set custom HTTP Request header fields my $xml; my $req = HTTP::Request->new(POST => $server_endpoint); $req->header('content-type' => 'text/xml'); # for descending order use # my @files = reverse(glob("*.xml")); my @files = (glob("*.xml")); print "@files\n"; my $test; foreach $test(@files){ #print "this element: $test \n "; open FILE, $test or die $!; while(<FILE>){ $xml .= $_; #print $xml; } } $req->content($xml); my $resp = $ua->request($req); if ($resp->is_success){ my $message = $resp->decoded_content; print "Received reply:\n $message\n"; } else { print "HTTP POST error code: ", $resp->code, "\n"; print "HTTP POST error message: ", $resp->message, "\n"; }
twig_rcvr_server
#!/usr/bin/perl use strict; use warnings; use CGI; use DBI; use XML::Twig; use CGI::Carp 'fatalsToBrowser'; my $cgi = CGI->new; print $cgi->header(-type => "text/xml", -charset => "utf-8"); my $xml = $cgi->param("POSTDATA"); print "the data $xml is received"; my $username = "abcxyz"; my $password = "xyzabc"; my $dsn = "dbi:mysql:twig:127.0.0.1"; my $dbh = DBI->connect($dsn,$username,$password) or die "Cannot connec +t to database: $DBI::errstr"; my $sql1 = 'INSERT INTO twig_test(name,ppg,rpg,apg,g,blk) VALUES(?,?,?,?,?,?)'; my $sth1 = $dbh->prepare($sql1); my $sql2 = 'INSERT INTO customer(first_name,last_name,dob,email) VALUES(?,?,?,?)'; my $sth2 = $dbh->prepare($sql2); my $sql3 = 'INSERT INTO supplier(name,address,tel_no) VALUES(?,?,?)'; my $sth3 = $dbh->prepare($sql3); #test if($dbh){ print "print successfully connected to the database"; } my $twig = new XML::Twig( twig_handlers => {player => \&player, customer => \&customer, supplier => \&supplier} ); $twig->parsefile($xml); #$twig->print; sub player {my ($twig,$player) = @_; my @f=(); $f[0] = $player->field('name'); $f[1] = $player->field('ppg'); $f[2] = $player->field('rpg'); $f[3] = $player->field('apg'); $f[4] = $player->field('g'); $f[5] = $player->field('blk'); print $f[0], "\n"; print $f[1], "\n"; print $f[2], "\n"; print $f[3], "\n"; print $f[4], "\n"; print $f[5], "\n"; #print "\n"; $sth1->execute(@f) or die $DBI::errstr; } sub customer {my ($twig,$customer) = @_; my @f=(); $f[0] = $player->field('first_name'); $f[1] = $player->field('last_name'); $f[2] = $player->field('dob'); $f[3] = $player->field('email'); print $f[0], "\n"; print $f[1], "\n"; print $f[2], "\n"; print $f[3], "\n"; #print "\n"; $sth2->execute(@f) or die $DBI::errstr; } sub supplier {my ($twig,$supplier) = @_; my @f=(); $f[0] = $player->field('name'); $f[1] = $player->field('address'); $f[2] = $player->field('tel-no'); print $f[0], "\n"; print $f[1], "\n"; print $f[2], "\n"; #print "\n"; $sth3->execute(@f) or die $DBI::errstr; }

Replies are listed 'Best First'.
Re: Parsing and inserting XML data
by tangent (Parson) on Jun 03, 2014 at 17:57 UTC
    This may or may not be the cause of your problem but in 'twig_rcvr_server' you have:
    $twig->parsefile($xml);
    But you are not parsing a file you are parsing the XML content so that should be:
    $twig->parse($xml);
Re: Parsing and inserting XML data
by taint (Chaplain) on Jun 03, 2014 at 08:59 UTC

    In other words; does your web server log indicate any problems. Because your file(s) don't have the correct Read, Write, or in the case of Perl scripts, Execute bits set (attributes). This is probably the most common case for HTTP-500 errors. If you're on a *NIX OS, chmod, and chown may provide further clues.

    Best wishes.

    --Chris

    ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

Re: Parsing and inserting XML data
by Mr. Muskrat (Canon) on Jun 03, 2014 at 19:02 UTC

    There may be other problems but here are the ones that I saw.

    In twig_client_parse, you are reading the contents of three XML files and concatenating them all into one string. That string is then sent in a POST to twig_rcvr_server.cgi where it is then passed to XML::Twig's parsefile method but parsefile expects a filename. You could use parse instead however it doesn't like concatenated files. So in twig_client_parse, you should process each file in turn. Also, in the customer and supplier subroutines of twig_rcvr_server.cgi you are trying to use $player again instead of $customer and $supplier respectively.

    Here are my untested recommendations. I have cleaned up your code ever so slightly but there is still room for improvement.

    # twig_client_parse use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $server_endpoint = "http://localhost/cgi-bin/RnD/twig/twig_rcvr_ser +ver.cgi"; # set custom HTTP Request header fields my $xml; my $req = HTTP::Request->new(POST => $server_endpoint); $req->header('content-type' => 'text/xml'); my @files = glob( "*.xml" ); #print "@files\n"; for my $test ( @files ) { #print "this element: $test \n "; my $xml = do { local( @ARGV, $/ ) = $test; <> }; $req->content( $xml ); my $resp = $ua->request( $req ); if ( $resp->is_success ) { my $message = $resp->decoded_content; print "Received reply:\n $message\n"; } else { print "HTTP POST error code: ", $resp->code, "\n"; print "HTTP POST error message: ", $resp->message, "\n"; } }
    # twig_rcvr_server.cgi use strict; use warnings; use CGI; use DBI; use XML::Twig; use CGI::Carp 'fatalsToBrowser'; my $cgi = CGI->new; print $cgi->header(-type => "text/xml", -charset => "utf-8"); my $xml = $cgi->param("POSTDATA"); print "the data $xml is received"; my $username = "abcxyz"; my $password = "xyzabc"; my $dsn = "dbi:mysql:twig:127.0.0.1"; my $dbh = DBI->connect($dsn,$username,$password) or die "Cannot connec +t to database: $DBI::errstr"; my $sql1 = 'INSERT INTO twig_test(name,ppg,rpg,apg,g,blk) VALUES(?,?,?,?,?,?)'; my $sth1 = $dbh->prepare($sql1); my $sql2 = 'INSERT INTO customer(first_name,last_name,dob,email) VALUES(?,?,?,?)'; my $sth2 = $dbh->prepare($sql2); my $sql3 = 'INSERT INTO supplier(name,address,tel_no) VALUES(?,?,?)'; my $sth3 = $dbh->prepare($sql3); #test print "print successfully connected to the database" if ( $dbh ); my $twig = new XML::Twig( twig_handlers => { player => \&player, customer => \&customer, supplier => \&supplier, } ); $twig->parse( $xml ); #$twig->print; sub player { my ( $twig, $player ) = @_; my @f = ( $player->field('name'), $player->field('ppg'), $player->field('rpg'), $player->field('apg'), $player->field('g'), $player->field('blk'), ); print $f[0], "\n"; print $f[1], "\n"; print $f[2], "\n"; print $f[3], "\n"; print $f[4], "\n"; print $f[5], "\n"; #print "\n"; $sth1->execute( @f ) or die $DBI::errstr; } sub customer { my ( $twig, $customer ) = @_; my @f = ( $customer->field('first_name'), $customer->field('last_name'), $customer->field('dob'), $customer->field('email'), ); print $f[0], "\n"; print $f[1], "\n"; print $f[2], "\n"; print $f[3], "\n"; #print "\n"; $sth2->execute( @f ) or die $DBI::errstr; } sub supplier { my ( $twig, $supplier ) = @_; my @f = ( $supplier->field('name'), $supplier->field('address'), $supplier->field('tel-no'), ); print $f[0], "\n"; print $f[1], "\n"; print $f[2], "\n"; #print "\n"; $sth3->execute( @f ) or die $DBI::errstr; }

    Update: Replaced semicolons with commas in @f in the player and customer subroutines. Oops.

Re: Parsing and inserting XML data
by Anonymous Monk on Jun 03, 2014 at 08:33 UTC
    What was the error message in the error log at the other end?