#!/usr/bin/env perl use strict; use warnings; use XML::Twig; use Data::Dumper; sub print_book { my ( $twig, $book ) = @_; my %this_book = map { $_ -> tag, $_ -> text } $book -> children; print Dumper \%this_book; $twig -> purge; } my $twig = XML::Twig -> new ( 'twig_handlers' => { 'Book' => \&print_book } ); $twig -> parsefile ( 'your_xml_file' ); #### $VAR1 = { 'Title' => 'The age of rocks', 'Author' => 'Mary', 'Released' => '8/16/1944' }; #### sub handle_node { my ( $twig, $element ) = @_; unless ( $element -> has_children ) { print "(", $element -> parent -> tag, ") ", $element -> tag, ": ", $element -> text,"\n"; } $twig -> purge; } my $twig = XML::Twig -> new ( 'twig_handlers' => { '_all_' => \&handle_node } ); $twig -> parsefile ( 'yourfile'); #### (Book) Title: The book of books (Book) Author: Sally (Book) Released: 1/2/2008 (Book) Title: The page of pages (Book) Author: Amanda (Book) Released: 6/3/1998 (Book) Title: The book of pages (Book) Author: John (Book) Released: 6/22/1963 (Book) Title: The rock of ages (Book) Author: Frank (Book) Released: 5/21/2004 (Book) Title: The age of rocks (Book) Author: Mary (Book) Released: 8/16/1944