I have dumped the following XML structure.
$VAR1 = {
'events' => {},
'docvalues' => {
'docvalue' => {
'ENGLAND' => {
'doc' => {
'England' => {
'value1' => '0.70312',
'value2' => '52.16045',
'type' => 'other',
'rank' => '21'
},
'New England District' => {
'value1' => '151.65',
'value2' => '-30.51667',
'type' => 'other',
'rank' => '18'
}
},
'id' => 'rb5'
},
'MS' => {
'contains' => 'rb7',
'abbrev-for' => 'Mississippi',
'doc' => {
'Mississip pi' => {
'value1' => '31.64850330352783',
'value2' => '-91.29143524169922',
'type' => 'other',
'rank' => '8'
},
'Mississippi County' => {
'value1' => '-89.31674',
'value2' => '36.81672',
'type' => 'other',
'rank' => '6'
}
},
'id' => 'rb9'
}
}
}
};
I’m stuck with how to extract the values from the value1 and value2 attributes. I tried using XML::Simple, but ending up with hash values rather than attributes.
my $doclist = XMLin('my file.xml');
my $docvalues = $doclist->{docvalues};
my @docvalue = $docvalues->{docvalue};
my ($v1, $v2, $v3) = @_;
foreach my $doc_value (@docvalue) {
my @doc = $doc_value->{doc};
foreach my $values (@doc) {
$v1 = $values->{'value1'};
}
}
It’s rather straight forward, but a little long.
Output:
The trick is to check where things reoccur. It’s clear that there seem to be several countries, so we need a loop for those. Then each country has something called
docwith two regions in it. We have to loop over these because they hold thevalue1andvalue2.Reindenting to increase redability and zooming out in the text editor (smaller font) helped me.