如何使用Perl从XML中重复标记中提取属性?

时间:2010-01-04 08:47:23

标签: xml perl

我正在尝试解析一组XML文件。 XML文件可以包含单个标记标记或多个标记标记:

<job>   <flag exit="0">aaa</flag> </job>

OR

<job>
  <flag exit="0">aaa</flag>
  <flag exit="1">bbb</flag>
  <flag exit="2">ccc</flag>
</job>

但确定这个“旗帜”计数必须在飞行中确定。确定标志计数并在其中打印值的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

use XML::Simple;
use Data::Dumper;

# This reads the data after the __DATA__ pragma
# into an array, which is then joined with no spaces
# to build the string $xml    
my $xml = join '', <DATA>;

# XMLin takes a filename, string or an IO::Handle object
# and slurps that data appropriately into a hash structure
my $config = XMLin($xml);

# Just look at the structure...
# print Dumper $config;

my $tag_count = @{$config->{flag}};

# As suggested in a comment below, 
# an alternative is to force the structure to be array based
# even for single elements, with XMLin($xml, ForceArray => 1);
if ($tag_count > 1) {
    print $_->{content}, q{ } for @{$config->{flag}}; # => aaa bbb ccc
    print $_->{exit},    q{ } for @{$config->{flag}}; # => 0 1 2
}
else {
    print $config->{flag}{content}; # => aaa
    print $config->{flag}{exit};    # => 0
}


__DATA__
<job>
    <flag exit="0">aaa</flag>
    <flag exit="1">bbb</flag>
    <flag exit="2">ccc</flag>
</job>

答案 1 :(得分:1)

您可以使用XML::SimpleForceArray选项强制在数组中提取每个标记或某些标记。