Bugz has asked for the wisdom of the Perl Monks concerning the following question:

Thank You for your replies monks, I was getting the hash as an input from a file with incorrect structuring. Anyways, the structure was corrected and I am now getting the Hash structure as
$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' } },
Using XML::Simple, I was able to convert it to basic XML as follows
<?xml version = "1.0"?> <data> <AA> <Description></Description> <Type>Module</Type> <File>Sample1.pl</File> </AA> <BB> <Description>Initiator</Description> <Type>Methods</Type> <File>Sample1.pl</File> </BB> <CC> <Description>Destructor</Description> <Type>Methods</Type> <File>Sample2.pl</File> <Value> <1>Ignore</1> <2>Retry</2> <3>Abort</3> </Value> </C> </data>
As you can see the XML tags <1> <2> <3> would be illegal, so I want to convert the data to something like following:
<?xml version = "1.0"?> <data> <Node No='1' Label='AA'> <Description></Description> <Type>Module</Type> <File>Sample1.pl</File> </Node> <Node No= '2' Label='BB'> <Description>Initiator</Description> <Type>Methods</Type> <File>Sample1.pl</File> </Node> <Node No='3' Label='CC'> <Description>Destructor</Description> <Type>Methods</Type> <File>Sample2.pl</File> <Value No='1'>Ignore</Value> <Value No='2'>Retry</Value> <Value No='3'>Abort</Value> </Node> </data>
I am unsure of how XML::Writer or XML::Twig could be able to parse the Perl Hash into the XML structure I need. Any Suggestions?

Replies are listed 'Best First'.
Re: Help with Perl and XML
by leonardohelman (Initiate) on Apr 25, 2008 at 20:00 UTC
    Hey, this is your problem, what do you want to do?.
    my %Fields = ( 'filename' =>{ Type => 'A'{ Name => 'Abstract'{ }; }; ....
    What is this? The syntax should be something like
    %F = ( aa => { BB => 'cc' }, ll => { NN => 'xx', MM => 'yy', )
    Here "use strict; use warnings;" is your friend (and Data::Dumper also) And also
    perldoc perldsc
    Leonardo Helman Pert Consultores SRL Argentina
Re: Help with Perl and XML
by mr_mischief (Monsignor) on Apr 25, 2008 at 19:57 UTC
    The syntax where you declare %Fields is immediately suspect. What exactly do you expect, for example, Type => 'A'{ to produce as a data structure?

    Perhaps a review of perldsc, perlref, and perlreftut would help you with that. While you're at it, you might want to look at perlstyle.

Re: Help with Perl and XML
by apl (Monsignor) on Apr 25, 2008 at 19:58 UTC
    What version of Perl are you using? Mine is fairly old, but your perlstruct.pl doesn't look validly constructed to me. This is confirmed when I try running it with use strict; use warnings;.

    Type => 'A'{ Name => 'Abstract'{ }; }; Type => 'B'{ Name => 'Preface'{ }; };

    especially looks strange.