Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Fetch field values from API output

by rkrish (Acolyte)
on Dec 12, 2012 at 10:40 UTC ( #1008484=perlquestion: print w/ replies, xml ) Need Help??
rkrish has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have an API output looks like :

<?xml version="1.0" encoding="ISO-8859-1" ?> - <envelope> - <body> - <outputGetUsageSummary> - <usgSumm rerateDone="5"> - <usageAccum accumId="269" accumCaptn="VD_DP_AR" inclUnits="9999999.0 +0" inclUnitsUsed="0.00" shared="false" pooled="false" prorated="false +" realtimeSbscrpShared="false" inclUnitsRolledOver="0.00" maxRollover +Units="0.00" inclUnitsToBeRolledOver="0.00" rolloverAvailableFlag="fa +lse" prevCycleReleasedFlag="false" sbscrpUsedUnits="0.00"> <prices svcPriceSeqNbr="00000RH2LS808H7" svcName="VD_DP_AR" captnTex +t="Vm Deposit Free Airtime" inclUnits="9999999.00" inclUnitsUsed="0.0 +0" ntwrkUnitsIncl="599999940.00" ntwrkUnitsUsed="0.00" maxAvailNtwrkU +nits="599999940.00" displayUnitsIncl="9999999.20" displayUnitsUsed="0 +.00" maxAvailDisplayUnits="9999999.20" ntwrkUnitId="1" displayUnitId= +"3" /> </usageAccum> - <usageAccum accumId="270" accumCaptn="VD_IN_AR" inclUnits="9999999.0 +0" inclUnitsUsed="0.00" shared="false" pooled="false" prorated="false +" realtimeSbscrpShared="false" inclUnitsRolledOver="0.00" maxRollover +Units="0.00" inclUnitsToBeRolledOver="0.00" rolloverAvailableFlag="fa +lse" prevCycleReleasedFlag="false" sbscrpUsedUnits="0.00"> <prices svcPriceSeqNbr="00000RH2LT888H8" svcName="VD_IN_AR" captnTex +t="Vm Deposit Free Airtime" inclUnits="9999999.00" inclUnitsUsed="0.0 +0" ntwrkUnitsIncl="599999940.00" ntwrkUnitsUsed="0.00" maxAvailNtwrkU +nits="599999940.00" displayUnitsIncl="9999999.20" displayUnitsUsed="0 +.00" maxAvailDisplayUnits="9999999.20" ntwrkUnitId="1" displayUnitId= +"3" /> </usageAccum> - <usageAccum accumId="1962" accumCaptn="DACC_AR:FREE:UNLIM FREE" incl +Units="9999999.00" inclUnitsUsed="0.00" shared="false" pooled="false" + prorated="false" realtimeSbscrpShared="false" inclUnitsRolledOver="0 +.00" maxRolloverUnits="0.00" inclUnitsToBeRolledOver="0.00" rolloverA +vailableFlag="false" prevCycleReleasedFlag="false" sbscrpUsedUnits="0 +.00"> <prices svcPriceSeqNbr="8085" svcName="DACC_AR" captnText="Dir Assis +t Call Completion Airtime" inclUnits="9999999.00" inclUnitsUsed="0.00 +" ntwrkUnitsIncl="599999940.00" ntwrkUnitsUsed="0.00" maxAvailNtwrkUn +its="599999940.00" displayUnitsIncl="9999999.20" displayUnitsUsed="0. +00" maxAvailDisplayUnits="9999999.20" ntwrkUnitId="1" displayUnitId=" +3" /> </usageAccum> </usgSumm> </outputGetUsageSummary> </body> </envelope
I need to fetch the values of 'inclUnits',' inclUnitsUsed','shared' from the output for each usageAccum accumId ('269','270','1962')..I have these values stored in an array..Please help me how the other values I mentioned above can be fetched from API output with the accumId's I have.

Comment on Fetch field values from API output
Download Code
Re: Fetch field values from API output
by roboticus (Canon) on Dec 12, 2012 at 10:53 UTC

    rkrish:

    Since you have the values in an array, I'd fetch the values from the array.

    Slightly more seriously: Since you have an XML document, you might investigate one of the many XML parsers found on http://www.cpan.org.

    Anyway, on to a real answer: I have no idea what your difficulty is, since you didn't describe your problem, and you didn't show the code where you read the values into the array. All I can do is guess...

    Guess 1: To get your values into the array, you just read it from a file and have no idea what to do with it. If that's the case, go to CPAN and find an XML parser and start digging in. Or you could post the code you're having trouble with.

    Guess 2: You're somehow calling a web service with perl without knowing anything about perl, XML or SOAP. In that case, I'd start researching and probably the person who helped you get to where you are now. If you can't reach them, you might try posting the code you're having trouble with.

    Guess 3: You're calling a web service with perl and you do know perl and a few other things, and are just having trouble with a SOAP library. Well, in that case, if you post the code you're having trouble with, someone might be able to help you find the problem.

    Well, that about does it for my guesses. If none of those are helpful, you might try this: Post the code giving you the problems, and ask a coherent question, and perhaps someone will be able to help you out.

    ...roboticus

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

      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.

        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.

Re: Fetch field values from API output
by choroba (Abbot) on Dec 14, 2012 at 06:36 UTC
    A solution using XML::XSH2, a wrapper around XML::LibXML.
    #!/usr/bin/perl use warnings; use strict; use feature qw(say); use XML::XSH2; @XML::XSH2::Map::accum_ids = (269, 270, 1962); xsh << '__XSH__'; open 1008484.xml ; for my $id in { @accum_ids } { cd /envelope/body/outputGetUsageSummary/usgSumm/usageAccum[@ac +cumId=$id] ; my $attrs = (@inclUnits | @inclUnitsUsed | @shared) ; perl { push @results, $attrs } ; } __XSH__ for my $result (@XML::XSH2::Map::results) { say join ' ', map $_->value, @$result; }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008484]
Approved by halfcountplus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2014-11-28 03:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (192 votes), past polls