$VAR1 = {
'nativeCurrencyCode' => 'USD',
'displayCurrencyCode' => 'USD',
'chargeableRoomRateTaxesAndFees' => '159.77',
'rateFrequency' => 'B',
'chargeableRoomRateTotal' => '1106.62',
'NativeNightlyRates' => {
'nativeNightlyRate' => [
'298.95',
'298.95',
'348.95'
],
'size' => '3'
},
'DisplayNightlyRates' => {
'size' => '3',
'displayNightlyRate' => [
'298.95',
'298.95',
'348.95'
]
},
'displayRoomRate' => '1106.62',
'nativeRoomRate' => '1106.62'
};
####
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use XML::Twig;
my $twig= XML::Twig->new( twig_handlers => { nativeNightlyRate => sub { content_to_att($_, 'rate'); },
displayNightlyRate => sub { content_to_att($_, 'rate'); },
DisplayNightlyRates => sub { $_->erase },
NativeNightlyRates => sub { $_->erase },
}
);
my $xml=qq~
USD
298.95
298.95
348.95
1106.62
1106.62
159.77
USD
298.95
298.95
348.95
1106.62
B
~;
$twig->parse( $xml); # build the twig
my $struct = $twig->simplify(
forcearray => [ qw(NativeNightlyRate DisplayNightlyRate) ],
);
print Dumper $struct;
sub content_to_att
{ my( $elt, $att)= @_;
$elt->set_att( $att => $elt->text)->cut_children;
}
####
$VAR1 = {
'nativeCurrencyCode' => 'USD',
'displayCurrencyCode' => 'USD',
'chargeableRoomRateTaxesAndFees' => '159.77',
'rateFrequency' => 'B',
'chargeableRoomRateTotal' => '1106.62',
'displayNightlyRate' => [
{
'rate' => '298.95'
},
{
'rate' => '298.95'
},
{
'rate' => '348.95'
}
],
'nativeNightlyRate' => [
{
'rate' => '298.95'
},
{
'rate' => '298.95'
},
{
'rate' => '348.95'
}
],
'displayRoomRate' => '1106.62',
'nativeRoomRate' => '1106.62'
};