use warnings; use strict; use XML::Twig; my $str = ' Book 1 author 1 Book 1 author 2 Book 1 title Book1ISBN Book 2 author 1 Book 2 author 2 Book 2 title Book2ISBN Book 3 author 1 Book 3 author 2 Book 3 author 3 Book 3 title Book3ISBN '; my $t = XML::Twig->new( twig_handlers => { book => \&book }, ); $t->parse($str); sub book { my ($t, $book) = @_; print $book->field('title'), ": "; print $book->att('type'), "\n"; } __END__ Book 1 title: technical Book 2 title: fiction Book 3 title: technical