0
I have the data structure like
$VAR1 = '<opt>
<GetBalanceIcici averageCost="3.3" lastMeterReadDateTime="2014-07-   01T11:35:00.000+05:30"      remainingDays="0">
<balances>
<balanceInfo accountId="2" balance="68" balanceId="1" balanceName="E-Mobile"    balanceType="EUR" paymentType="OTHER" precision="6" refDateTime="2014-07-01T17:25:45.000+05:30" rolloverCounter="0">
<pockets>
<pocket endDateTime="2015-08-01T00:00:00.000+05:30" partialBalance="15" startDateTime="2014-07-01T00:00:00.000+05:30" />
<pocket endDateTime="2015-07-01T00:00:00.000+05:30" partialBalance="53" startDateTime="2014-06-01T00:00:00.000+05:30" />
<pocket partialBalance="0" />
</pockets>
</balanceInfo>
<balanceInfo accountId="2" balance="1.04" balanceId="2" balanceName="E-Credit_EUR" balanceType="Eur" paymentType="PREPAID" pockets="" precision="6" refDateTime="2014-07-01T17:25:45.000+05:30" rolloverCounter="0" />
<balanceInfo accountId="2" balance="-68" balanceId="4" balanceName="E-GCounter" balanceType="kWh" paymentType="OTHER" pockets="" precision="6" refDateTime="2014-07-01T17:25:45.000+05:30" rolloverCounter="0" />
<balanceInfo accountId="2" balance="-14.96" balanceId="5" balanceName="E-MCredit_EUR" balanceType="Eur" paymentType="OTHER" precision="6" refDateTime="2014-07-01T17:25:45.000+05:30" rolloverCounter="0">
<pockets>
<pocket endDateTime="2015-08-01T00:00:00.000+05:30" partialBalance="-3.3" startDateTime="2014-07-01T00:00:00.000+05:30" />
<pocket endDateTime="2015-07-01T00:00:00.000+05:30" partialBalance="-11.66" startDateTime="2014-06-01T00:00:00.000+05:30" />
<pocket partialBalance="0" />
</pockets>
</balanceInfo>
</balances>
</GetBalanceIcici>
</opt>
';

I want to print like

Account Id is 2 balance is 68 Balance name is E-Mobile balanceType is EUR payment type is OTHER precision is 6 Pockets is( endDateTime="2015-08-01T00:00:00.000+05:30",partialBalance="-11.66",startDateTime="2014-06-01T00:00:00.000+05:30"

I have written the below mentioned code but not able to capture properly

my $xml1 = XMLin($xml);
print "\n----------------------------\n";
print Dumper $xml1;
print "\n----------------------------\n";
foreach my $e ($xml1->{'GetBalanceIcici'}) {
    foreach my $f ($e->{'balances'}) {
        foreach my $g  ($f->{'balanceInfo'}) {
            foreach my $j (@{$g}) {
                print  "Account Id is\t ". $j->{'accountId'} ."\tbalance is\t" .$j->{'balance'}.
"\tBalance  name is\t" . $j->{'balanceName'} ."\tbalanceType is\t". $j->{'balanceType'} .
"\tpayment type is\t".$j->{'paymentType'} ."\tprecision is \t".$j->'precision'}."\t ."\t       pocket is...."."\n";
            }
        }
    }
1
  • Yes there was a typo as I manually typed .Corrected . Commented Jul 1, 2014 at 12:34

1 Answer 1

1

I advise you not to use XML::Simple for processing XML.

Instead use more modern modules like XML::LibXML and XML::Twig.

Below is an attempt to recreate your parsing using the first of these two mdoules:

use strict;
use warnings;

use XML::LibXML;

my $xml = XML::LibXML->load_xml(IO => \*DATA);

for my $node ($xml->findnodes('//GetBalanceIcici/balances/balanceInfo')) {
    my @label_to_key = (
        ['Account Id',   'accountId'],
        ['balance',      'balance'],
        ['Balance name', 'balanceName'],
        ['balanceType',  'balanceType'],
        ['payment type', 'paymentType'],
        ['precision',    'precision'],
    );

    print join(' ', map {"$_->[0] is " . $node->getAttribute($_->[1])} @label_to_key), "\n";
}

__DATA__
<opt>
<GetBalanceIcici averageCost="3.3" lastMeterReadDateTime="2014-07-   01T11:35:00.000+05:30"      remainingDays="0">
<balances>
<balanceInfo accountId="2" balance="68" balanceId="1" balanceName="E-Mobile"    balanceType="EUR" paymentType="OTHER" precision="6" refDateTime="2014-07-01T17:25:45.000+05:30" rolloverCounter="0">
<pockets>
<pocket endDateTime="2015-08-01T00:00:00.000+05:30" partialBalance="15" startDateTime="2014-07-01T00:00:00.000+05:30" />
<pocket endDateTime="2015-07-01T00:00:00.000+05:30" partialBalance="53" startDateTime="2014-06-01T00:00:00.000+05:30" />
<pocket partialBalance="0" />
</pockets>
</balanceInfo>
<balanceInfo accountId="2" balance="1.04" balanceId="2" balanceName="E-Credit_EUR" balanceType="Eur" paymentType="PREPAID" pockets="" precision="6" refDateTime="2014-07-01T17:25:45.000+05:30" rolloverCounter="0" />
<balanceInfo accountId="2" balance="-68" balanceId="4" balanceName="E-GCounter" balanceType="kWh" paymentType="OTHER" pockets="" precision="6" refDateTime="2014-07-01T17:25:45.000+05:30" rolloverCounter="0" />
<balanceInfo accountId="2" balance="-14.96" balanceId="5" balanceName="E-MCredit_EUR" balanceType="Eur" paymentType="OTHER" precision="6" refDateTime="2014-07-01T17:25:45.000+05:30" rolloverCounter="0">
<pockets>
<pocket endDateTime="2015-08-01T00:00:00.000+05:30" partialBalance="-3.3" startDateTime="2014-07-01T00:00:00.000+05:30" />
<pocket endDateTime="2015-07-01T00:00:00.000+05:30" partialBalance="-11.66" startDateTime="2014-06-01T00:00:00.000+05:30" />
<pocket partialBalance="0" />
</pockets>
</balanceInfo>
</balances>
</GetBalanceIcici>
</opt>

Outputs:

Account Id is 2 balance is 68 Balance name is E-Mobile balanceType is EUR payment type is OTHER precision is 6
Account Id is 2 balance is 1.04 Balance name is E-Credit_EUR balanceType is Eur payment type is PREPAID precision is 6
Account Id is 2 balance is -68 Balance name is E-GCounter balanceType is kWh payment type is OTHER precision is 6
Account Id is 2 balance is -14.96 Balance name is E-MCredit_EUR balanceType is Eur payment type is OTHER precision is 6
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.