in reply to Re^3: Perl and XML
in thread Perl and XML

The Bigger Picture:

I am extracting the data from a set of perl files and storing it in the hash of hashes structure as described above in the sample data. the structure of the hash is described as
$functions = { 'function-name' => { 'Function-Description' = 'Description' 'Function-Type' = 'Type' 'Function-Location' = 'File in which function is located' 'Values to be passed to function' => { '1' = 'Value 1 Description', ... ... 'n' = 'Value n Description'} }, ... ... 'function-name' => { 'Function-Description' = 'Description' 'Function-Type' = 'Type' 'Function-Location' = 'File in which function is located' 'Values to be passed to function' => { '1' = 'Value 1 Description' ... ... 'n' = 'Value n Description'} } }

I have to now represent it to the users and allow them to search for the Functions they require and return details of the functions that match. The presentation happens over web as a webpage that shall
Thus I wanted was converting it to XML so that I could then use XML and possibly AJAX for returning suggested matches when a user searches for a function.

Now if you are familiar with XSL, you do know that you can create a stylesheet that allows you to described the style information for each node.

But in the XML conversion that grandfather suggested, the node names are the actual function names and with more than 1000 different functions existing, it would not be practical to create a stylesheet with description for each function-node. Thus I wished To convert it in the format I suggested.

Again, I am new to perl thus I am unsure how I can do this in one time when the first perl-to-xml conversion happens rather than to repeating it as this functionality I intend to take it live and thus would require to happen during every maintenance cycle.

I hope that would be descriptive enough to explain my concern with perl-to-xml conversion.
Bugz.

Replies are listed 'Best First'.
Re^5: Perl and XML
by GrandFather (Saint) on May 05, 2008 at 23:01 UTC

    So you want something like:

    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, 'Function'); $twig->print (); sub AddElements { my ($parent, $hash, $type) = @_; for my $node (sort keys %$hash) { my $child; if ($type) { $child = $parent->insert_new_elt (last_child => $type, {'Name' => $node}); } elsif ($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> <Function Name="AA"> <Description></Description> <File>Sample1.pl</File> <Type>Module</Type> </Function> <Function Name="BB"> <Description>Initiator</Description> <File>Sample1.pl</File> <Type>Methods</Type> </Function> <Function Name="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> </Function> </Fields>

    Perl is environmentally friendly - it saves trees