use strict; use warnings; use XML::TreeBuilder; my $xml = < Contents of a - 1 Contents of b Contents of a - 2 Contents of c Contents of d XML my $root = XML::TreeBuilder->new (); $root->parse ($xml); print "All the 'a' elements\n"; for my $elt ($root->look_down ('_tag' => 'a')) { print $elt->as_XML (), "\n"; } print "All the 'b' elements\n"; for my $elt ($root->look_down ('_tag' => 'b')) { print $elt->as_XML (), "\n"; } print "All the 'a' elements with attr='2'\n"; for my $elt ($root->look_down ('_tag' => 'a', 'attr' => '2')) { print $elt->as_XML (), "\n"; }