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

Ok before I get downtrodden I have used supersearch and have checked out numerous modules on CPAN. My question is I have a HASH such as
%DATA = ( 'link2' => { 'attribute' => { 'x' => '0050', 'y' => '0050', 'r' => '100' }, 'src' => 'http://foo.com/4158.swf', 'colours' => { 'alpha' => '000' }, 'name' => 'Future truth oracle' } }
And from this hash I want to generate a XML tree such as
<link2> <attribute> <x>0050</x> <y>0050</x> <r>100</r> </attribute> <src>http://foo.com/4158.swf</src> etc.
I have looked at modules such as Data::DumpXML, however this outputs a XML tree to describe the actual hash, not the key value pairs within it. Does anyone know of a module which which will generate a xml tree to meet my requirements ? Thanks

Replies are listed 'Best First'.
(Ovid) Re: Hash to XML
by Ovid (Cardinal) on Mar 26, 2002 at 18:52 UTC
    use strict; use XML::Simple; my $xs = new XML::Simple; my %DATA = ( 'link2' => { 'attribute' => { 'x' => '0050', 'y' => '0050', 'r' => '100' }, 'src' => 'http://foo.com/4158.swf', 'colours' => { 'alpha' => '000' }, 'name' => 'Future truth oracle' } ); # it's the 'noattr' option that is important here my $xml = $xs->XMLout(\%DATA, noattr => 1); print $xml;

    Output:

    <opt> <link2> <attribute> <x>0050</x> <y>0050</y> <r>100</r> </attribute> <src>http://foo.com/4158.swf</src> <colours> <alpha>000</alpha> </colours> <name>Future truth oracle</name> </link2> </opt>

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Superb, Did the trick nicely !! Many thanks folks !!
Re: Hash to XML
by perrin (Chancellor) on Mar 26, 2002 at 18:45 UTC
    Check out XML::Simple. The XMLOut() method should do what you want.