0

我试图使用 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\))//;
    }
}
4

1 回答 1

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;
}
于 2013-11-09T04:16:59.383 回答