#!/bin/perl -w use strict; use XML::Twig; my $twig = XML::Twig->new(); $twig->parse( \*DATA) or die "could not parse!"; # the #ELT argument means that we will get only the # "real" elements, as opposed to the ones containing the text foreach my $tag($twig->descendants( '#ELT')) { print $tag->gi; print " ", $tag->text if( is_field( $tag)); print "\n"; } sub is_field { my $tag= shift; return 1 if( ($tag->children == 1) && $tag->first_child->is_text); return 0; } __DATA__ elt 1 subelt 1 subelt 2 subelt 3 elt 3 subelt 4