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.


In reply to Re: Parsing and inserting XML data by Mr. Muskrat
in thread Parsing and inserting XML data by Anonymous Monk

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.