pjotrik is right, the example was not XML. If it were, this is a solution using XML::Rules (AZed, neither this solution nor XML::Twig has any memory problems whatsoever with this. Not all XML parsers construct a maze of objects before you can get your hands on the data.)

#!/usr/bin/perl use strict; use warnings; use XML::Rules; print "This will calculate the total amount and To no of transactions +\n"; my $totalamt = 0; my $cnt = 0; my $parser = XML::Rules->new( stripspaces => 7, rules => { _default => '', b => sub {++$cnt;return}, payment_amount => sub {$totalamt+=$_[1]->{_content};return}, }); $parser->parse(\*DATA); print "The total amount found is $totalamt \n"; print "Total Transactions are $cnt \n"; __DATA__ <a> <b> <client_account_cred>68789790390909090489</client_account_ +cred> <Payment_UTR_No>MTRIN10909890896</Payment_UTR_No> <payment_amount>700000</payment_amount> </b> <b> <client_account_cred>9033753053985392INR</client_account_c +red> <Payment_UTR_No>938573895735154</Payment_UTR_No> <payment_amount>1222706</payment_amount> </b> <b> <client_account_cred>9284723472047222INR</client_account_c +red> <Payment_UTR_No>RP JLLKL7687</Payment_UTR_No> <payment_amount>1437865.95</payment_amount> </b> </a>
or if you do not want to use file scoped lexicals (and want the parser to be able to give you the right answer even if you call it several times):
#!/usr/bin/perl use strict; use warnings; use XML::Rules; print "This will calculate the total amount and To no of transactions +\n"; my $parser = XML::Rules->new( stripspaces => 7, start_rules => { a => sub { $_[4]->{pad}{count}=0; $_[4]->{pad}{total}=0; 1; } }, rules => { _default => '', b => sub {++ $_[4]->{pad}{count}; return}, payment_amount => sub {$_[4]->{pad}{total} += $_[1]->{_content +};return}, a => sub {return %{$_[4]->{pad}}} }); my $data = $parser->parse(\*DATA); print "The total amount found is $data->{total}\n"; print "Total Transactions are $data->{count}\n"; __DATA__ <a> <b> ...

In reply to Re: Counting XML Blocks in XML file and Calculating Total Amount by Jenda
in thread Counting XML Blocks in XML file and Calculating Total Amount by harishnuti

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.