#!/usr/bin/perl use strict; use warnings; use XML::LibXML; use Data::Dumper 'Dumper'; my $xml = XML::LibXML->new(); my $doc = $xml->parse_file( "j.xml" ); my $pth = XML::LibXML::XPathContext->new( $doc ); my @subblocks; for my $sbnode ( $pth->findnodes( "/itpslsig/itpslbody/mainblock/subblock" )) { for my $sbchild ( $sbnode->childNodes ) { next unless ( $sbchild->nodeName =~ /fileexists|userexec/ ); my %feitem = ( STARTOP => $sbchild->nodeName ); for my $fechild ( $sbchild->childNodes ) { $feitem{$fechild->nodeName} = $fechild->textContent; } my @features = map { "$_ => $feitem{$_}" } sort grep /^\w/, keys %feitem; push @subblocks, [ @features ]; } } print Dumper( \@subblocks );