#!/bin/perl -w use strict; use XML::Twig; my @results; # does not have to be global, it's just easier # create the twig see the docs for why to use TwigRoots my $t= new XML::Twig( TwigRoots => { elt => \&check_elt }); # call check_elt every time an element elt is parsed $t->parse( \*DATA); # parse the XML (use parsefile to parse... a file) print join "\n", @results; print "\n"; sub check_elt { my( $t, $elt)= @_;) # $t is the XML::Twig object # $elt is an XML::Twig::Elt object if( my $child= $elt->first_child( 'child')) # that's how you navigate the element { push @results, $child->text; } # text includes sub elements of child else { push @results, "missing child for elt " . $elt->att( 'id'); } $t->purge; # call only if your document is huge } # to free the memory __DATA__ I am a child 1 child 2