in reply to Re^2: trying to understand xml::twig and also trying to learn how to extract attribute
in thread trying to understand xml::twig and also trying to learn how to extract attribute

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);
  • Comment on Re^3: trying to understand xml::twig and also trying to learn how to extract attribute
  • Download Code

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

    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
Re^4: trying to understand xml::twig and also trying to learn how to extract attribute
by toolic (Bishop) on Nov 05, 2008 at 21:02 UTC
    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' ];
      this gives me much better idea and also give me more idea for further code.. will get back to you soon.. thank you.
        can I just specify first_child of any element within the root?
        first_child ($optional_condition)

        Return the first child of the element, or the first child matching the $optional_condition

        for e.g., given below xml, in order to get to <getthis value="found" />, do I always have to go
        if ($one->first_child('traffic')->first_child('something')->fir +st_child('somethingelse')->first_child('getthis')->att('value') eq 'f +ound') { push @ids, $one->att('id'); push @ids, $one->true('yes'); }
        OR can I just pick the element like
        if ($one->first_child('getthis')->att('value') eq 'found') { push @ids, $one->att('id'); push @ids, $one->true('yes'); }
        Another words, is first_child definition the child of the root?(in this case , 'one' and then 'traffic' and then so on.. and therefore need to go down the chain in steps or can I say my parent is one and then first child is 'getthis' ?
        <one id="msn" type="shopping"> <traffic> <daily value="on" /> <weekly value="off" /> <something> <somethingelse> <getthis value="found" true="yes" /> <somethingelse> </something> <monthly value="off" /> </traffic> </one>