in reply to Re^3: XML::Twig Help
in thread XML::Twig Help

I REALLY appreciate your help team...

OK, that seems to help me understand handlers better but what about situations where the xml tag has multiple children of the same name? When attempting to use the handler as below I only get the first TYPE element.

i.e.

use strict; use XML::Twig; my $xmlstr = <<EOF; <TREES> <APPLE> <TYPE id="1001">Scarlet</TYPE> <TYPE id="1002">Red</TYPE> <TYPE id="1003">Gold</TYPE> <TYPE id="1004">Fuji</TYPE> <TYPE id="1005">Scarlet</TYPE> </APPLE> <PEAR> <TYPE id="1001">Bradford</TYPE> <TYPE id="1002">Cleveland Select</TYPE> <TYPE id="1003">Aristocrat</TYPE> <TYPE id="1004">Autumn Blaze</TYPE> </PEAR> </TREES> EOF my $twig = XML::Twig->new(twig_handlers => {APPLE => \&apple}); $twig->parse($xmlstr); sub apple { my ($t, $elt) = @_; print $elt->att('id'), "\n"; print $elt->field('TYPE' ), "\n"; print "\n"; }

Replies are listed 'Best First'.
Re^5: XML::Twig Help
by toolic (Bishop) on Jan 07, 2011 at 17:19 UTC
    use warnings; use strict; use XML::Twig; my $xmlstr = <<EOF; <TREES> <APPLE> <TYPE id="1001">Scarlet</TYPE> <TYPE id="1002">Red</TYPE> <TYPE id="1003">Gold</TYPE> <TYPE id="1004">Fuji</TYPE> <TYPE id="1005">Scarlet</TYPE> </APPLE> <PEAR> <TYPE id="1001">Bradford</TYPE> <TYPE id="1002">Cleveland Select</TYPE> <TYPE id="1003">Aristocrat</TYPE> <TYPE id="1004">Autumn Blaze</TYPE> </PEAR> </TREES> EOF my $twig = XML::Twig->new(twig_handlers => {APPLE => \&apple}); $twig->parse($xmlstr); sub apple { my ($t, $elt) = @_; for my $type ($elt->children('TYPE')) { print $type->att('id'), "\n"; print $type->text(), "\n"; print "\n"; } } __END__ 1001 Scarlet 1002 Red 1003 Gold 1004 Fuji 1005 Scarlet

    Use warnings too.