我已经转储了以下XML结构。
$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'
}
}
}
};
我坚持如何从value1
和value2
属性中提取值。我尝试使用XML::Simple,但最后是哈希值而不是属性。
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'};
}
}
答案 0 :(得分:1)
这是相当直接的,但有点长。
foreach my $country (keys %{ $VAR1->{'docvalues'}->{'docvalue'} } ) {
print "Country: $country\n";
foreach my $doc (keys %{ $VAR1->{'docvalues'}->{'docvalue'}->{$country}->{'doc'} }) {
print "doc: $doc\n";
print "value1: " . $VAR1->{'docvalues'}->{'docvalue'}->{$country}->{'doc'}->{$doc}->{'value1'} . "\n";
print "value2: " . $VAR1->{'docvalues'}->{'docvalue'}->{$country}->{'doc'}->{$doc}->{'value2'} . "\n";
}
}
输出:
Country: ENGLAND
doc: England
value1: 0.70312
value2: 52.16045
doc: New England District
value1: 151.65
value2: -30.51667
Country: MS
doc: Mississip pi
value1: 31.64850330352783
value2: -91.29143524169922
doc: Mississippi County
value1: -89.31674
value2: 36.81672
诀窍是检查事情再次发生的地方。很明显,似乎有几个国家,所以我们需要一个循环。然后每个国家/地区都有一个名为doc
的内容,其中包含两个区域。我们必须循环使用这些内容,因为它们包含value1
和value2
。
在文本编辑器(较小的字体)中重新加强以增加可更改性和缩小帮助了我。
my $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'
}
}
}
};
答案 1 :(得分:0)
您可以通过这种方式获取值:
print "$ref->{docvalues}->{docvalue}->{ENGLAND}->{doc}->{England}->{value1}\n";
print "$ref->{docvalues}->{docvalue}->{ENGLAND}->{doc}->{England}->{value2}\n";
print "$ref->{docvalues}->{docvalue}->{ENGLAND}->{doc}->{'New England District'}->{value1}\n";
print "$ref->{docvalues}->{docvalue}->{ENGLAND}->{doc}->{'New England District'}->{value2}\n";
print "$ref->{docvalues}->{docvalue}->{MS}->{doc}->{Mississippi}->{value1}\n";
print "$ref->{docvalues}->{docvalue}->{MS}->{doc}->{Mississippi}->{value2}\n";