To: Poj 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; }

In reply to Re: unable to pass data by Anonymous Monk
in thread unable to pass data by ashishg0310

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.