customer.xml
Frank
Sanbeans
3/10
frank@example.com
####
nba.xml
Houston, Allan6920.13.42.814
Sprewell, Latrell6919.24.54.015
Ewing, Patrick4914.610.01.068
####
supplier.xml
Supplier 1
Address 1
012345
####
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_server.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(){
$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";
}
####
#!/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 connect 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;
}