You could post the xml to the server and then process with XML::Twig
and assign different code block to the different elements. Something like this;
Client code
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $server_endpoint = "http://localhost/test/process2.cgi";
# set custom HTTP request header fields
my $req = HTTP::Request->new(POST => $server_endpoint);
$req->header('content-type' => 'application/json');
my $xml = do { local $/; <DATA> };
$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";
}
__DATA__
<?xml version="1.0"?>
<data>
<customer-data>
<customer>
<first_name>Frank</first_name>
<last_name>Sanbeans</last_name>
<dob>3/10</dob>
<email>frank@example.com</email>
</customer>
<customer>
<first_name>Sandy</first_name>
<last_name>Sanbeans</last_name>
<dob>4/15</dob>
<email>sandy@example.com</email>
</customer>
</customer-data>
<supplier-data>
<supplier>
<name>Supplier 1</name>
<address>Address 1</address>
<tel-no>012345</tel-no>
</supplier>
</supplier-data>
</data>
Server code
#!perl
use strict;
use CGI;
use DBI;
#use CGI::Carp 'fatalsToBrowser';
use XML::Twig;
my $dbh = dbh(); # connect
# customer database
my $sql1 = 'INSERT INTO c_details (f_name,l_name,dob,email)
VALUES (?,?,?,?)';
my $sth1 = $dbh->prepare($sql1);
# supplier database
my $sql2 = 'INSERT INTO s_details (name,address,tel_no)
VALUES (?,?,?)';
my $sth2 = $dbh->prepare($sql2);
my $cgi = CGI->new;
print $cgi->header(-type => "application/json", -charset => "utf-8");
my $xml = $cgi->param("POSTDATA");
# process file
my $t = XML::Twig->new(
twig_handlers => {
'customer' => \&customer,
'supplier' => \&supplier, }
);
$t->parse($xml);
sub customer {
my ($t,$elt) = @_;
my @f=();
$f[0] = $elt->field('first_name');
$f[1] = $elt->field('last_name');
$f[2] = $elt->field('dob');
$f[3] = $elt->field('email');
$sth1->execute(@f) or die $DBI::errstr;
print join "\t",$sql1,@f,"\n";
}
sub supplier {
my ($t,$elt) = @_;
my @f=();
$f[0] = $elt->field('name');
$f[1] = $elt->field('address');
$f[2] = $elt->field('tel-no');
$sth2->execute(@f) or die $DBI::errstr;
print join "\t",$sql2,@f,"\n";
}
poj |