#!/usr/bin/perl use strict; use warnings; use XML::Twig; use Data::Dumper; my $xml = do{ local $/; }; my @hosts; my $parser = XML::Twig->new( twig_handlers => { 'HOST' => \&hosts, }); $parser->parse($xml); print Dumper \@hosts; sub hosts { my ($twig,$host) = @_; my %hash = ( ip => $host->findvalue( 'IP' ), dns => $host->findvalue( 'DNS' ), comment => $host->findvalue( 'COMMENT/VALUE' ), ); my @attrs = $host->findnodes( 'USER_DEFINED_ATTR_LIST/USER_DEFINED_ATTR' ); for my $attr ( @attrs ) { my $index = $attr->findvalue( 'UDA_INDEX' ); next unless $index == 2; $hash{attr} = $attr->findvalue( 'UDA_VALUE' ); } push @hosts, \%hash; }