my $ref = XMLin($data, KeyAttr => { charge => "code"}); my %hash = map { ($_, $ref->{charge}->{$_}->{name}) } keys %{$ref->{charge}}; $ref = \%hash;