Extracting specific nodes from XML using XML::Twig

Posted by pratz on Stack Overflow See other posts from Stack Overflow or by pratz
Published on 2013-11-08T15:25:58Z Indexed on 2013/11/08 15:54 UTC
Read the original article Hit count: 379

Filed under:
|
|

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>
        <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\))//;
    }
}

© Stack Overflow or respective owner

Related posts about Xml

Related posts about perl