in reply to Perl and XML
It's not clear to me why the xx tags need to be transformed. If that is not an issue then the following which uses XML::Twig to generate an XML fragment given a hash as described may be of help:
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}); } }
Prints:
<Fields> <AA> <Description></Description> <File>Sample1.pl</File> <Type>Module</Type> </AA> <BB> <Description>Initiator</Description> <File>Sample1.pl</File> <Type>Methods</Type> </BB> <CC> <Description>Destructor</Description> <File>Sample2.pl</File> <Type>Methods</Type> <Values> <Value No="1">Ignore</Value> <Value No="2">Retry</Value> <Value No="3">Abort</Value> </Values> </CC> </Fields>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl and XML
by Bugz (Acolyte) on Apr 30, 2008 at 19:00 UTC | |
by shmem (Chancellor) on Apr 30, 2008 at 21:58 UTC | |
by GrandFather (Saint) on Apr 30, 2008 at 23:38 UTC | |
by Bugz (Acolyte) on May 05, 2008 at 16:52 UTC | |
by GrandFather (Saint) on May 05, 2008 at 23:01 UTC |