我试图使用XML :: Twig从以下XML结构中提取一组特定节点,但此后一直困扰着。我需要从以下结构中提取“播放器”节点,并对每个节点值执行字符串匹配/替换。
<pep:record>
<agency type="player">
<subrecord type="scout">
<isnum>123XXX (print)</isnum>
<isnum>234YYY (mag)</isnum>
</subrecord>
<subrecord type="group">
</subrecord>
</agency>
</record>
我尝试使用以下代码,但我指向哈希引用而不是实际字符串。
my $parser = XML::Twig->new(twig_handlers => {
isnum => sub { print $_->text."::" },
});
foreach my $rec (split(/::/, $parser->parse($my_xml))) {
if ($rec =~ m/print/) {
($print = $rec) =~ s/( \(print\))//;
}
elsif($rec =~ m/mag/) {
($mag = $rec) =~ s/( \(mag\))//;
}
}
答案 0 :(得分:1)
据我所知,你需要这样的东西:
use Modern::Perl;
use XML::Twig;
my $my_xml = <<EOL;
<pep:record>
<agency type="player">
<subrecord type="scout">
<isnum>123XXX (print)</isnum>
<isnum>234YYY (mag)</isnum>
</subrecord>
<subrecord type="group">
</subrecord>
</agency>
</pep:record>
EOL
my @records;
my $parser = XML::Twig->new(twig_handlers => {
isnum => sub { push @records, $_->text },
});
$parser->parse($my_xml);
foreach my $rec (@records) {
if ($rec =~ m/print/) {
$rec =~ s/( \(print\))//;
}
elsif ($rec =~ m/mag/) {
$rec =~ s/( \(mag\))//;
}
say $rec;
}