w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How do I extract an attribute/property in Perl using XML::Twig module?

This is one way to get 100. It uses the first_child method:

use warnings;
use strict;
use XML::Twig;

my $xml = <<XML;
<note>
    <to _Id="100">Share</to>
    <from>Jane</from>
    <heading>Reminder</heading>
    <body>A simple text</body>
</note>
XML

my $twig = XML::Twig->new(twig_handlers => { note => &getId
});
$twig->parse($xml);

sub getId {
    my ($twig, $mod) = @_;
    my $to_id = $mod->first_child('to')->att('_Id');
    print "$to_id 
";
}




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.