#! /usr/bin/env perl use XML::Simple; use Data::Dumper; use strict; use warnings; ######## ########### Variables ######## # List of info we want for each message entry my @keysToKeep = ('date', 'body', 'address', 'type'); # XML Parser my $xml = new XML::Simple; ######## ########### Subroutines ######## # Filepath is passed to load a hashref # a CLEANED Hashref is returned of the xml we've loaded sub loadFileToHashRef { my $xmlRef = $xml->XMLin($_[0]); # Clean our hash for my $textRef ( @{$xmlRef->{sms}} ) { foreach my $key (keys %{$textRef}) { # If your $key is *not* what we're looking for, remove it from the hash if ( !grep ($key, @keysToKeep) ) { delete $textRef->{$key}; } } } # Return our hash ref return $xmlRef; } my $hashRef = loadFileToHashRef("xml/sms-20110704030000.xml"); print Dumper $hashRef;