use XML::Twig; my $t = XML::Twig->new(PrettyPrint => 'record'); $t->parsefile('adj.xml'); my $root = $t->root; # @pair has all the pairs of adjacent cities in it my @pair = $root->children; # target city we are looking for my $city = 'menlo park'; # this routine takes a search text and a list of XML elements and # searches them for the text sub candidate_generator { my ($search_text, @data) = @_; grep { grep { $_->text eq $search_text } $_->children } @data; } # take the entire XML-base and search for records which have our # target city in them my @adj = candidate_generator($city,@pair); # print them out in a human-readable form map { $_->print } @adj;