Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: trying to understand xml::twig and also trying to learn how to extract attribute

by toolic (Bishop)
on Nov 05, 2008 at 01:43 UTC ( [id://721546]=note: print w/replies, xml ) Need Help??


in reply to trying to understand xml::twig and also trying to learn how to extract attribute

Here is a different approach using XML::Twig. I consider this a more brute-force, less elegant solution than that provided by GrandFather. However, it illustrates the TIMTOWTDI-ness of Twig; perhaps you are more comfortable thinking of this as looping through elements, rather than the more powerful use of handlers (as I currently am). I have borrowed GrandFather's cleaned up XML text.
use strict; use warnings; use Data::Dumper; use XML::Twig; my $xmlStr = <<XML; <config> <one id="msn" type="shopping"> <traffic> <daily value="on" /> <weekly value="off" /> <monthly value="off" /> </traffic> </one> <one id="movies" type="entertainment"> <traffic> <daily value="on" /> <weekly value="off" /> <monthly value="on" /> </traffic> </one> <one id="espn" type="sports"> <traffic> <daily value="on" /> <weekly value="on" /> <monthly value="on" /> <hyper value="true" /> </traffic> </one> </config> XML my @ids; my $t = XML::Twig->new(); $t->parse($xmlStr); for my $one ($t->root()->children('one')) { push @ids, $one->att('id'); } print Dumper(\@ids); __END__ $VAR1 = [ 'msn', 'movies', 'espn' ];
  • Comment on Re: trying to understand xml::twig and also trying to learn how to extract attribute
  • Download Code

Replies are listed 'Best First'.
Re^2: trying to understand xml::twig and also trying to learn how to extract attribute
by GrandFather (Saint) on Nov 05, 2008 at 02:05 UTC

    I should have noted that the cleaned up XML is licensed under the Perl license so use it by all means. ;)


    Perl reduces RSI - it saves typing
      thank you guys
      Grandfather's solution is *bit* above my head at this point
      But eventually, I like my code to look like that(once I have better understanding)
      so I tried the second solution with bit of modification, but the way I am doing is I am sure wrong(as it's not working)
      I wanted to loop through and find monthly value eq 'on' and then push a certain elements and attribute in the array which I can loop through later for usage...
      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use XML::Twig; my $xmlStr = <<XML; <config> <one id="msn" type="shopping"> <traffic> <daily value="on" /> <weekly value="off" /> <monthly value="off" /> </traffic> </one> <one id="movies" type="entertainment"> <traffic> <daily value="on" /> <weekly value="off" /> <monthly value="on" /> </traffic> </one> <one id="espn" type="sports"> <traffic> <daily value="on" /> <weekly value="on" /> <monthly value="on" /> <hyper value="true" /> </traffic> </one> </config> XML my @ids; my $t = XML::Twig->new(); $t->parse($xmlStr); for my $one ( ($t->root()->children('one') ) { for my $month ( ($t->root()->children('monthly') ) { if ( $momth->att('value') eq 'on' ) { push @ids, $one->att('id'); } } } print Dumper(\@ids);

        If you ask specific questions about the code you don't understand you may get answers that help. If you have trouble with the code so too will others - by asking you help many people.


        Perl reduces RSI - it saves typing
        Your problem is that "monthly" is not a child of root ("config"); it is a child of "traffic". I think you want this:
        my @ids; my $t = XML::Twig->new(); $t->parse($xmlStr); for my $one ($t->root()->children('one')) { if ($one->first_child('traffic')->first_child('monthly')->att('val +ue') eq 'on') { push @ids, $one->att('id') } } print Dumper(\@ids); __END__ $VAR1 = [ 'movies', 'espn' ];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-18 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found