use XML::Simple; # your structure $FIELDS = { 'AA' => { 'Description' => '', 'Type' => 'Module', 'File' => 'Sample1.pl', }, 'BB' => { 'Description' => 'Initiator', 'Type' => 'Methods', 'File' => 'Sample1.pl', }, 'CC' => { 'Description' => 'Destructor', 'Type' => 'Methods', 'File' => 'Sample2.pl', 'Values' => { '1' => 'Ignore', '2' => 'Retry', '3' => 'Abort', } } }; # hash transformation, in-place my $c; for my $key (sort keys %$FIELDS) { $FIELDS->{$key}->{Label} = $key; $FIELDS->{$key}->{No} = ++$c; for my $label (qw(Description Type File)) { $FIELDS->{$key}->{$label} = { content => $FIELDS->{$key}->{$label} }; } if (exists $FIELDS->{$key}->{Values}) { my $values_hash = $FIELDS->{$key}->{Values}; $FIELDS->{$key}->{Values} = [ map { { No => $_, content => $values_hash->{$_} } } sort keys %$values_hash ]; } } # output the XML print XMLout( { data => { Node => [ map $FIELDS->{$_}, sort keys %$FIELDS ] } } ); __END__ Sample1.pl Module Initiator Sample1.pl Methods Destructor Sample2.pl Methods Ignore Retry Abort