Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Need XMLTwig help

by poj (Parson)
on Aug 22, 2013 at 20:36 UTC ( #1050560=note: print w/replies, xml ) Need Help??

in reply to Need XMLTwig help

my $t = XML::Twig->new( twig_handlers => { 'Phones'=> \&phones } ); $t->parse($xml); sub phones { my ($t,$elt) = @_; print $elt->parent->att('name');; print ' Phone = ',$t->last_elt('Phone[@name="cell"]') ->att('number'),"\n"; print "\n"; }

Replies are listed 'Best First'.
Re^2: Need XMLTwig help
by McA (Priest) on Aug 22, 2013 at 21:00 UTC

    Being not the XPath expert, poj's post (++) animated to reduce my solution to the following:

    #!/usr/bin/env perl use 5.010; use strict; use warnings; use Data::Dumper; use local::lib 'lib'; use XML::Twig; my $string = <<EOF; <Person name="Brian"> <Phones> <Phone name="cell" number="5551112233" /> <Phone name="cell" number="5557778899" /> <Phone name="home" number="5554441122" /> </Phones> </Person> EOF my $twig = XML::Twig->new; $twig->parse($string); my @nodes = $twig->findnodes('/Person/Phones/Phone[@name="cell"]'); my @cellphones_nrs = map { $_->att('number') } @nodes; say "Cellphone: $cellphones_nrs[-1]" if @cellphones_nrs;


Re^2: Need XMLTwig help
by perlvroom (Acolyte) on Aug 23, 2013 at 12:53 UTC
    Thank you! This helped a ton.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1050560]
[choroba]: Progress! Nowadays, you can write a Perl script to generate such an anagram!
[GotToBTru]: i like to order my chemicals a-la-carte ambrus ;)
[thepkd]: Hi, in trying to create an Array of Hash of Array of Hash. You read it right. Is it possible to dereference it without using temps?

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2016-12-06 13:09 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (104 votes). Check out past polls.