in reply to Perl and XML

Reading the XML::Simple man page helps. Write your expected xml to a file, say, foo.xml, then read and dump it with a script named foo:

use XML::Simple; use Data::Dumper; $Data::Dumper::Indent = 1; my $config = XMLin(); print Dumper($config);

Voilà:

$VAR1 = { 'Node' => [ { 'Type' => 'Module', 'No' => '1', 'File' => 'Sample1.pl', 'Description' => {}, 'Label' => 'AA' }, { 'Type' => 'Methods', 'No' => '2', 'File' => 'Sample1.pl', 'Description' => 'Initiator', 'Label' => 'BB' }, { 'Value' => [ { 'No' => '1', 'content' => 'Ignore' }, { 'No' => '2', 'content' => 'Retry' }, { 'No' => '3', 'content' => 'Abort' } ], 'Type' => 'Methods', 'No' => '3', 'File' => 'Sample2.pl', 'Description' => 'Destructor', 'Label' => 'CC' } ] };

Lather, rinse, repeat.

--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}

Replies are listed 'Best First'.
Re^2: Perl and XML
by Bugz (Acolyte) on Apr 29, 2008 at 17:33 UTC
    Hey,
    The Data I provided as Perl Hash is a sample of what I have. And I need to convert it the other way round, from Hash to XML and XML::Simple will just convert it to simple XML but then the problem of illegal tags <1>,<2>,<3> crops up so i can't use XML::Simple unless it allows me to make that changes.
    Bugz

      Did you lather, rinse, repeat? You would have found out that the data structure you want with XML::Simple is as follows:

      $FIELDS = { data => { Node => [ { No => '1', Label => 'AA', Type => { content => 'Module' }, File => { content => 'Sample1.pl'}, Description => { }, }, { No => '2', Label => 'BB', Type => { content => 'Methods' }, File => { content => 'Sample1.pl' }, Description => { content => 'Initiator' }, }, { No => '3', Label => 'CC', Type => { content => 'Methods' }, File => { content => 'Sample2.pl' }, Description => { content => 'Destructor' }, Value => [ { No => '1', content => 'Ignore', }, { No => '2', content => 'Retry', }, { No => '3', content => 'Abort', } ], } ] } };

      Output:

      <opt> <data> <Node Label="AA" No="1"> <Description></Description> <File>Sample1.pl</File> <Type>Module</Type> </Node> <Node Label="BB" No="2"> <Description>Initiator</Description> <File>Sample1.pl</File> <Type>Methods</Type> </Node> <Node Label="CC" No="3"> <Description>Destructor</Description> <File>Sample2.pl</File> <Type>Methods</Type> <Value No="1">Ignore</Value> <Value No="2">Retry</Value> <Value No="3">Abort</Value> </Node> </data> </opt>

      Getting rid of the outer <opt> tag is left as an exercise to the reader.

      As dragonchild wrote above, you need to transform your hash so as to fit the module you are using.

      --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}
        Hey Shmem
        I think I made my questions confusing. I apologise. Let me rephrase my question: I have a Perl Hash as shown
        $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' } } },
        I need to convert it to a Valid XML File. I tried using XML::Simple, but it changes '1' to <1> which is invalid XML Tag.
        I checked CPAN docs on XML::Simple, and while they talk about KeyAttr ValueAttr, I am unsure How I can do what you and dragonchild suggest.
        I wanted to know if I could handle this conversion better using XML::Writer or XML::Twig and if so how?
        Thanks,
        Bugz