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

In reply to Re^2: unable to pass data by poj
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.