perlvroom has asked for the wisdom of the Perl Monks concerning the following question:
Ok, the following script does what i need, but i was hoping someone could school me on how to do this more effeciently:
<settings key="a" value="1"/>
<settings key="b" value="2"/>
The goal is to declare the "key" attribute as a variable with it's value equal to the "value" attribute in the same element, for all "settings" elements.
XML snippet:use XML::Twig; my $t= XML::Twig->new( twig_handlers => { 'settings' => \&file, } ) ->parsefile( 'sample.xml'); sub file{ my($t, $settings)=@_; foreach($settings->att('key')){ if($settings->att('key') eq 'a'){ $a = $settings->att('value'); } if($settings->att('key') eq 'b'){ $b = $settings->att('value'); } } }
<settings key="a" value="1"/>
<settings key="b" value="2"/>
The goal is to declare the "key" attribute as a variable with it's value equal to the "value" attribute in the same element, for all "settings" elements.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Creating a hash out of multiple XML attributes in an elment
by mirod (Canon) on Jul 14, 2013 at 19:34 UTC | |
by perlvroom (Acolyte) on Jul 14, 2013 at 21:39 UTC | |
by mirod (Canon) on Jul 15, 2013 at 02:02 UTC | |
by perlvroom (Acolyte) on Jul 15, 2013 at 14:50 UTC | |
by Jenda (Abbot) on Jul 15, 2013 at 15:03 UTC | |
by Anonymous Monk on Jul 14, 2013 at 23:32 UTC | |
Re: Creating a hash out of multiple XML attributes in an elment
by Jenda (Abbot) on Jul 15, 2013 at 15:20 UTC |
Back to
Seekers of Perl Wisdom