CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Need to get (Key,Value) from XML based on previous element (Key,Value) data

 



rafay
New User

May 26, 2011, 3:00 AM

Post #1 of 4 (5058 views)
Need to get (Key,Value) from XML based on previous element (Key,Value) data Can't Post

Based on the below sample xml, I need to write a perl script which will fetch all ‘TOTALAMOUNT’ value, however I also need to put a condition on previous element key ‘INVOICEDATE’ based on which ‘TOTALAMOUNT’ can be retrived.


Quote
<INVOICE INVOICENUMBER="007" INVOICEDATE="20110302" >
<TOTALAMTINTAX TOTALAMOUNT="1,27 "/> </INVOICE>

<INVOICE INVOICENUMBER="008" INVOICEDATE="20110402">
<TOTALAMTINTAX TOTALAMOUNT="1,47 "/> </INVOICE>

<INVOICE INVOICENUMBER="009" INVOICEDATE="20110402">
<TOTALAMTINTAX TOTALAMOUNT="5,27 "/> </INVOICE>

<INVOICE INVOICENUMBER="010" INVOICEDATE="20110502">
<TOTALAMTINTAX TOTALAMOUNT="6,27 "/> </INVOICE>



miller
User

May 26, 2011, 4:24 AM

Post #2 of 4 (5057 views)
Re: [rafay] Need to get (Key,Value) from XML based on previous element (Key,Value) data [In reply to] Can't Post

What xml parser are you using? I'd recommend XML::Twig.

- Miller


mirod
New User

May 27, 2011, 12:19 AM

Post #3 of 4 (5042 views)
Re: [rafay] Need to get (Key,Value) from XML based on previous element (Key,Value) data [In reply to] Can't Post

First, the INVOICE elements need to be wrapped in a root element, or the XML will not be well-formed.

Once that's done, the following code will do the job.

A couple of comments: to parse a file you will need to replace the call to parse by parsefile, your numbers are formatted the "European" way (with a comma before the decimal part), which is not the way Perl likes its numbers, so you need to convert them. Making the code robust is left as an exercise to the reader, since I can't really make assumptions about what is likely to go wrong with the data.

I hope that helps.


Code
#!/usr/bin/perl  

use strict;
use warnings;

use XML::Twig;

my $total_for_date= get_total_amount( '20110402');
print "$total_for_date\n";

sub get_total_amount
{ my( $date)= @_;

my $total=0;
# long line below, feel free to break it up!
XML::Twig->new( twig_handlers=> { qq{INVOICE[\@INVOICEDATE="$date"]} => sub { $total += att_to_nb($_->first_child( 'TOTALAMTINTAX')->att( 'TOTALAMOUNT')); } })
->parse( \*DATA);
return $total;
}

sub att_to_nb
{ my( $att)= @_;
$att=~ s{,}{.};
return $att;
}

__DATA__
<invoices>
<INVOICE INVOICENUMBER="007" INVOICEDATE="20110302" >
<TOTALAMTINTAX TOTALAMOUNT="1,27 "/> </INVOICE>

<INVOICE INVOICENUMBER="008" INVOICEDATE="20110402">
<TOTALAMTINTAX TOTALAMOUNT="1,47 "/> </INVOICE>

<INVOICE INVOICENUMBER="009" INVOICEDATE="20110402">
<TOTALAMTINTAX TOTALAMOUNT="5,27 "/> </INVOICE>

<INVOICE INVOICENUMBER="010" INVOICEDATE="20110502">
<TOTALAMTINTAX TOTALAMOUNT="6,27 "/> </INVOICE>
</invoices>



miller
User

May 27, 2011, 8:37 PM

Post #4 of 4 (5021 views)
Re: [rafay] Need to get (Key,Value) from XML based on previous element (Key,Value) data [In reply to] Can't Post

Just take another look at your thread on devshed to see an XML::Parser alternative solution to my original XML::Twig solution.

Help needed to get (Key,Value) from XML based on previous element (Key,Value) data

- Miller

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives