use XML::Twig; my $hash; my $t= XML::Twig->new( twig_handlers => { charge => sub{ $hash->{$_[1]->{'att'}->{'code'} }=$_[1]->{'att'}->{'name'}}, }, ); $t->parse( ' '); print $_."\t".$hash->{$_}."\n" for keys %{$hash};