Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^4: XML::Twig Help

by onegative (Scribe)
on Jan 07, 2011 at 17:08 UTC ( [id://881128]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://881128]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found