use XML::LibXML; my $xml = XML::LibXML->new->parse_fh(\*DATA); foreach my $station ($xml->findnodes('/servers/*')) { printf("Station: %s\n", $station->tagName); foreach my $ip ($station->findnodes('./ip')) { printf("\tIP: %s\n", $ip->textContent); } } __DATA__ 10.0.0.101 10.0.1.101 10.0.0.102 10.0.0.103 10.0.1.103 10.0.0.111 10.0.1.111 10.0.0.112 10.0.0.113 10.0.1.113 10.0.0.121 10.0.1.121 10.0.0.122 10.0.0.123 10.0.1.123 10.2.1.123