如何从XML :: Simple中提取数据结构中的属性?

时间:2021-09-27 14:30:36

I have dumped the following XML structure.

我已经转储了以下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'
            }
        }
    }
};

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.

我坚持如何从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'};
    }
}

2 个解决方案

#1


1  

It's rather straight forward, but a little long.

这是相当直接的,但有点长。

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";
  }
}

Output:

输出:

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

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 doc with two regions in it. We have to loop over these because they hold the value1 and value2.

诀窍是检查事情再次发生的地方。很明显,似乎有几个国家,所以我们需要一个循环。然后每个国家都有一个名为doc的东西,里面有两个区域。我们必须遍历这些因为它们包含value1和value2。


Reindenting to increase redability and zooming out in the text editor (smaller font) helped me.

在文本编辑器(较小的字体)中重新加入以增加可重复性和缩小对我有帮助。

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

#2


0  

You can get the values this way:

您可以通过这种方式获取值:

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

#1


1  

It's rather straight forward, but a little long.

这是相当直接的,但有点长。

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";
  }
}

Output:

输出:

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

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 doc with two regions in it. We have to loop over these because they hold the value1 and value2.

诀窍是检查事情再次发生的地方。很明显,似乎有几个国家,所以我们需要一个循环。然后每个国家都有一个名为doc的东西,里面有两个区域。我们必须遍历这些因为它们包含value1和value2。


Reindenting to increase redability and zooming out in the text editor (smaller font) helped me.

在文本编辑器(较小的字体)中重新加入以增加可重复性和缩小对我有帮助。

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

#2


0  

You can get the values this way:

您可以通过这种方式获取值:

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