One approach is to use Twig handlers. For example:
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";
}
|