Reputation: 358
I was trying to extract a particular set of nodes from the following XML structure using XML::Twig, but have been stuck ever since. I need to extract the 'player' nodes from the following structure and do a string match/replace on each of these node values.
<pep:record>
<agency type="player">
<subrecord type="scout">
<isnum>123XXX (print)</isnum>
<isnum>234YYY (mag)</isnum>
</subrecord>
<subrecord type="group">
</subrecord>
</agency>
</record>
I tried using the following code, but I get pointed to a hash reference rather than actual string.
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\))//;
}
}
Upvotes: 0
Views: 295
Reputation: 10666
As I understand you need something like this:
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;
}
Upvotes: 1