Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Fetch field values from API output

by rkrish (Acolyte)
on Dec 13, 2012 at 12:46 UTC ( [id://1008657]=note: print w/replies, xml ) Need Help??


in reply to Re: Fetch field values from API output
in thread Fetch field values from API output

The code I'm having is :

sub rerateandupdate (@) { my $currAcct = shift; my $getAccumsForAcct = qq{ select distinct usage_acum_seq_nbr fro +m svc_price sp,svc_agrmnt_line_item sali,sbscrp_Asgm sa where sp.free +_usage_across_acct_ind in ('Y','A') and sp.svc_name=sali.svc_name and + sp.comm_svc_area_id=sali.comm_svc_area_id and sp.charge_type_cd='F' +and sp.svc_price_expr_dt is null and svc_agrmnt_trmntn_dt is null an +d sali.sbscrp_id=sa.sbscrp_id and sa.sbscrp_asgm_expr_dt is null and +acct_nbr=:currAcct}; my $getAccums = $lda->prepare($getAccumsForAcct); if(defined($getAccums)) { $getAccums->execute(); while (my @accums = $getAccums->fetchrow_array() ) { foreach(@accums) { int returnCode = SUCCESS; my $xmlFile = "GetUsgSummary_" . $acct_nbr . ".xml +"; createXmlForUpd( $xmlFile, $acct_nbr, ); $returnCode = invokeServer( $acct_nbr , $xmlFi +le ); if ( $returnCode != SUCCESS ) { return $return +Code; } return $returnCode; } } } }
sub createXmlForUpd($$) { my $xmlFile = shift; my $acct_nbr = shift; my $returnCode = SUCCESS; # Generate XML API input file for UpdateAccount API open(INFILE,">$xmlFile")||die "Can not find file $xmlFile\n"; print INFILE '<envelope>' . "\n"; print INFILE '<header>' . "\n"; print INFILE '</header>' . "\n"; print INFILE '<body>' . "\n"; print INFILE '<inputGetUsageSummary acctNbr="' . $acct_nbr .'" bil +lUnbilledInd="U" "\n"; print INFILE "</body>\n"; print INFILE "</envelope>\n\n"; close(INFILE); return $returnCode; }
sub invokeServer($$) { my $acct_nbr = shift; my $xmlFile = shift; my $pwd = $ENV{'PWD'}; my $api_call = ${pwd} . "/REAccount1 -stdin < " . $xmlFile; my $cleanup_call = "rm ". $xmlFile; if ($opt_c eq "Y" || $opt_c eq "y") { my $output = `$api_call`; # Execute Unix command } else { print LOG_FILE "[$acct_nbr] NO_GO - XML Input file successfull +y created for acct_nbr = $acct_nbr -> API hasn't been invoked\n"; return SUCCESS; } }

in the subroutine invokeServer,from the 'output' of api call,I need to fetch the values I mentioned in my post and the output of XML api call I already posted.

Replies are listed 'Best First'.
Re^3: Fetch field values from API output
by roboticus (Chancellor) on Dec 13, 2012 at 18:32 UTC

    rkrish:

    In that case, I'd look into using XML::Twig to parse your XML document(s). Looking at it, it seems like the code should be pretty simple. I've not used it before, so I'm sure I'll get some details wrong, but I would think that something like this could do it:

    #!/usr/bin/perl use strict; use warnings; use XML::Twig; use Data::Dump; my @work_items; # Set up the parser my $twig = XML::Twig->new( twig_handlers => { usageAccum => \&gather_values, } } # Parse the file and gather work items $twig_parsefile('foo.xml'); # Process the work items for my $r (@work_items) { print Dumper($r), "\n"; } sub gather_values { # We just got a usageAccum node, so get the interesting attributes # and push them into the work item list. my $node = shift; push @work_items, { inclUnits => $node->{attr}->{inclUnits}; inclUnitsUsed => $node->{attr}->{inclUnitsUsed}; shared => $node->{attr}->{shared}; }; }

    I didn't test it, as I'm busy at the moment. But you should be able to turn this into something useful. If you have trouble, hopefully we can get an XML::Twig savvy monk to chime in with tips.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1008657]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-26 08:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found