use strict; use warnings; use XML::Twig; my $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' } } }; my $twig = XML::Twig->new (pretty_print => 'indented'); $twig->set_root (my $root = XML::Twig::Elt->new ('Fields')); AddElements ($root, $FIELDS); $twig->print (); sub AddElements { my ($parent, $hash) = @_; for my $node (sort keys %$hash) { my $child; if ($node =~ /^[0-9]/) { $child = $parent->insert_new_elt (last_child => 'Value', {No => $node}); } else { $child = $parent->insert_new_elt (last_child => $node); } if (! ref $hash->{$node}) { $child->set_text ($hash->{$node}); next; } AddElements ($child, $hash->{$node}); } } #### Sample1.pl Module Initiator Sample1.pl Methods Destructor Sample2.pl Methods Ignore Retry Abort