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

Hey GrandFather,
Thank you for the response. It does solve one of the problems and provides me exactly what i was looking to do. However, the reason changing the xx tag was to take care of the cases where multiple tags of the same xx value existing.
The perl hash i provided was a sample of the data i have and it has multiple xx values that repeat unfortunately, making it an illegal hash as it has multiple keys of same value, but that's how data is stored in it.
Thus I look forward to changing it to something like:

Option 1
<Function> <Name>XX</Name> ... ... </Function>

or

Option 2
<Function Name=""> ... ... </Function>

This way, I could ensure I was pulling in all of these instances.
Could you suggest something for the same?
Thanks,
Bugz

Replies are listed 'Best First'.
Re^3: Perl and XML
by shmem (Chancellor) on Apr 30, 2008 at 21:58 UTC

    Please put some effort into understanding the solutions provided in this thread. For further help provide us with the real data and the output you want to get, along with the script you use to achieve that goal. That would cut down guessing and annoyance. Tendyeberrymud.

    XY problem is always a good read.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re^3: Perl and XML
by GrandFather (Saint) on Apr 30, 2008 at 23:38 UTC

    In that case you really need a different data structure. Perhaps you ought give us a chunk of the bigger picture?


    Perl is environmentally friendly - it saves trees
      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
        1. Already have all this information.
        2. A search tool that returns only the information for a particular function that the user wants.

      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.

        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