如何解析xml并将哈希引用的值连接到字符串

时间:2014-07-01 12:10:46

标签: perl

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>
';

我想打印像

帐户ID为2余额为68余额名称为E-Mobile balanceType为EUR支付类型为OTHER精度为6个口袋为(endDateTime =&#34; 2015-08-01T00:00:00.000 + 05:30&#34 ;,partialBalance =&#34; -11.66&#34;,的startDateTime =&#34; 2014-06-01T00:00:00.000 + 05:30&#34;

我已经编写了下面提到的代码,但无法正确捕获

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 个答案:

答案 0 :(得分:1)

我建议您不要使用XML::Simple来处理XML。

而是使用更多现代模块,例如XML::LibXMLXML::Twig

下面是尝试使用这两个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>

输出:

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