my %books; my $twig= new XML::Twig( twig_handlers => { book => \&books } ); $twig->parse($xmlStr); print Dumper(\%books); exit; sub books { my ($twig, $book) = @_; my $id = $book->att('id'); $books{$id}{'author' } = $book->first_child('author' )->text(); $books{$id}{'title' } = $book->first_child('title' )->text(); $books{$id}{'genre' } = $book->first_child('genre' )->text(); $books{$id}{'price' } = $book->first_child('price' )->text(); $books{$id}{'publish_date' } = $book->first_child('publish_date')->text(); }