使用XML :: Simple时如何删除Magic Number?

Nan*_* HE 2 xml perl xml-simple xml-parsing

我做了这样的练习,如何通过XML :: Simple计算折叠成数组的XML元素的数量,这样我就不必对元素进行硬编码了?我计划使用代码来解析更大的xml文件.我不想通过手册来讨论这些元素.

我可以使用一些计数来代替幻数,有点像person.counthobbie.length等.我知道,我可以在C#中方便地使用这种说法.

#!/usr/bin/perl -w
use strict;
use XML::Simple;
use Data::Dumper;

my $tree = XMLin('./t1.xml');

print Dumper($tree);
print "\n";
for (my $i = 0; $i < 2; $i++) # magic number '2'
{
    print "$tree->{person}->[$i]->{first_name} $tree->{person}->[$i]->{last_name}\n";
    print "\n";
    for (my $j = 0; $j < 3; $j++) # magic number '3'
    {
        print $tree->{person}->[$i]->{hobbie}->[$j], "\n";
    }
    print "\n";
}
Run Code Online (Sandbox Code Playgroud)

出局:

could not find ParserDetails.ini in C:/Perl/site/lib/XML/SAX
$VAR1 = {
          'person' => [
                      {
                        'hobbie' => [
                                    'bungy jumping',
                                    'sky diving',
                                    'knitting'
                                  ],
                        'last_name' => 'Bloggs',
                        'first_name' => 'Joe'
                      },
                      {
                        'hobbie' => [
                                    'Swim',
                                    'bike',
                                    'run'
                                  ],
                        'last_name' => 'LIU',
                        'first_name' => 'Jack'
                      }
                    ]
        };

Joe Bloggs

bungy jumping
sky diving
knitting

Jack LIU

Swim
bike
run
Run Code Online (Sandbox Code Playgroud)

我的Xml源文件如下

<Document>
  <person>
    <first_name>Joe</first_name>
    <last_name>Bloggs</last_name>
    <hobbie>bungy jumping</hobbie>
    <hobbie>sky diving</hobbie>
    <hobbie>knitting</hobbie>
  </person>
  <person>
    <first_name>Jack</first_name>
    <last_name>LIU</last_name>
    <hobbie>Swim</hobbie>
    <hobbie>bike</hobbie>
    <hobbie>run</hobbie>
  </person>
</Document>
Run Code Online (Sandbox Code Playgroud)

DVK*_*DVK 5

由于XML :: Simple将为您生成一个数组,因此很容易计算它的长度.

Eg $tree->{person}是一个数组 - 或者说是一个数组引用(通过使用XML :: Simple的ForceArray选项确保它是一个,即使只有一个人).

  • 您可以通过首先将其取消引用到数组本身(使用@{}数组取消引用)来获取其长度:@{ $tree->{person} }

  • 然后在标量上下文中使用结果数组,该上下文计算数组中的元素数(换句话说,a.lenth/ a.count其他语言scalar(@a)中的scalar()函数转换为Perl惯用语,如果标量上下文已经应用,则函数是可选的).

    在这种情况下,数值比较运算符"<"将强制标量上下文,但如果不是这种情况,则可以使用scalar()函数.

例:

# Don't forget ForceArray option of XML::Simple to ensure person and hobbie are array refs
for (my $i = 0; $i < scalar( @{ $tree->{person} } ); $i++) { # scalar() is optional here
    print "$tree->{person}->[$i]->{first_name} $tree->{person}->[$i]->{last_name}\n";
    print "\n";
    for (my $j = 0; $j < @{ $tree->{person}->[$i]->{hobbie} }; $j++) {
        print $tree->{person}->[$i]->{hobbie}->[$j], "\n";
    }
    print "\n";
}
Run Code Online (Sandbox Code Playgroud)

注意,计算Perl数组长度的一种稍微不同的方法是$#a构造,它返回数组最后一个元素的索引 - 例如,比数组中元素的数量少1.我不知道使用这两种方法之间有任何性能差异,所以如果你发现它们同样可读,请在适当时使用它们(例如,如果你需要得到最后一个元素的索引,请使用$#a;如果是#of elements,则使用@ascalar(@a)作为需要).

Perl Data Structures Cookbook @perldoc是一个很好的参考