use strict; my $xs = new XML::Simple; my $xml = $xs->XMLin("$folder_path\\policies.xml", forcearray => 1, keyattr => ['policies']); my $hash_add = [{ $cat => [{ 'name' => "$name", 'link_name' => "$link_name", 'description' => "$desc", 'date' => "$today_date" }] }]; # Add to hash push (@{$xml->{'policies'}}, $hash_add); print "Content-type: text/html\n\n"; # Sort hash @{ $xml->{'policies'} } = sort { "\L$a->{name}" cmp "\L$b->{name}"} @{ $xml->{'policies'} }; use Data::Dumper; print Dumper($xml); $xs->XMLout($xml, outputfile => "$folder_path\\policies.xml", attrindent => 1);