my $t= XML::Twig->new( pretty_print => 'indented', twig_handlers => { 'product'=>sub{ my @pname = $_[1]->get_xpath('name'); my @pids = $_[1]->get_xpath('product_id'); print $pids[0]->text," - ",$pname[0]->text,"\n"; my %h; my @ids = $_[1]->get_xpath('attributes/attribute/group/id'); my @names = $_[1]->get_xpath('attributes/attribute/group/name'); @h{map {$_->text} @ids } = map {$_->text} @names ; my @vids = $_[1]->get_xpath('attributes/attribute/value/id'); my @values = $_[1]->get_xpath('attributes/attribute/value/value'); @h{map {$_->text} @vids } = map {$_->text} @values ; print map {"\t$_ - $h{$_}\n"} keys %h; print "\n\n"; } } ); $t->parse($xml); ####OUTPUT ABC123 - My product - 12.1998 1561 - Lġngd (i mm) 1507 - Engines 1498 - Year model 12033 - Vehicle equipment 12019 - Maybe 1518 - Year model (to) 301 - Generator XYZ789 - My product - 12.1992 1507 - Engines 1498 - Year model 1518 - Year model (to) 301 - Generator