in reply to XML::Twig Help
use warnings; use strict; use XML::Twig; my $xmlstr = <<EOF; <AGENT hostname="viper3"> <LADDER> <ACL> <ACCOUNT id="4cf031986c"> <USERNAME>emcon</USERNAME> <HOST>*sppcon*</HOST> <PERMISSION>CDOPS</PERMISSION> </ACCOUNT> <ACCOUNT id="b92794bbd7"> <USERNAME>cpemcon</USERNAME> <HOST>*</HOST> <PERMISSION>COPS</PERMISSION> </ACCOUNT> <ACCOUNT id="8ff0478641"> <USERNAME>dbemcon</USERNAME> <HOST>*</HOST> <PERMISSION>COPS</PERMISSION> </ACCOUNT> <ACCOUNT id="22d2647740"> <USERNAME>tuxemcon</USERNAME> <HOST>*</HOST> <PERMISSION>COPS</PERMISSION> </ACCOUNT> </ACL> </LADDER> </AGENT> EOF my $twig = XML::Twig->new( twig_handlers => {ACCOUNT => \&acct} ); $twig->parse($xmlstr); sub acct { my ($t, $elt) = @_; print $elt->att('id'), "\n"; print $elt->first_child('USERNAME' )->text(), "\n"; print $elt->first_child('HOST' )->text(), "\n"; print $elt->first_child('PERMISSION')->text(), "\n"; print "\n"; } __END__ 4cf031986c emcon *sppcon* CDOPS b92794bbd7 cpemcon * COPS 8ff0478641 dbemcon * COPS 22d2647740 tuxemcon * COPS
Update: There is a great tutorial at http://web.archive.org/web/20080702002018/http://xmltwig.com (unfortunately, the tutorial link from XML::Twig seems to be down).
Update: using mirod's suggestion, and refactoring a little...
sub acct { my ($t, $elt) = @_; print $elt->att('id'), "\n"; for my $tag (qw(USERNAME HOST PERMISSION)) { print $elt->field($tag), "\n"; } print "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML::Twig Help
by mirod (Canon) on Jan 07, 2011 at 14:35 UTC | |
by toolic (Bishop) on Jan 07, 2011 at 14:42 UTC | |
by onegative (Scribe) on Jan 07, 2011 at 17:08 UTC | |
by toolic (Bishop) on Jan 07, 2011 at 17:19 UTC | |
by onegative (Scribe) on Jan 10, 2011 at 21:31 UTC | |
by toolic (Bishop) on Jan 10, 2011 at 21:46 UTC | |
by onegative (Scribe) on Jan 10, 2011 at 21:51 UTC |