#!/bin/perl -w use strict; use XML::Twig; my $t= new XML::Twig( twig_handlers => { post => \&post }); $t->parse( \*DATA); sub post { my( $t, $post)= @_; # all handlers get called with those arguments # here is the magic! # gi is the element name and text is its... text! my %hash= map { $_->gi, $_->text} $post->children; # or whatever you want to do with the hash print "City: $hash{location} \n"; # if your file is small enough you don't need to purge, otherwise # it will free the memory used so far $t->purge; } __DATA__ 1234 somecity NJ 87922 Othercity, AK