http://www.perlmonks.org?node_id=1008485


in reply to Fetch field values from API output

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.

Replies are listed 'Best First'.
Re^2: Fetch field values from API output
by rkrish (Acolyte) on Dec 13, 2012 at 12:46 UTC

    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.