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

What I want to do is parse this hash to a xml file.
I want to be able to get the data from the xml file back into
the structure.

Is there any simple why to do this? If not can you point me to
a absolute beginners step by step, that can help me learn enough
to accomplish this?

The only difference in all the records is the number of items in
'seven'.
my %t_type = ( 'a' => { one => {low => 1000, high => 3000, percent => 25}, two => {low => 200,high => 2000, percent => 30}, three => {low => 1000,high => 6000, percent => 40}, four => {low => 300,high => 1800, percent => 35}, five => {low => 10, high => 40, percent => 60}, six => {low => 2, high => 12, percent => 50}, seven => [ {select => 'small',low => 3,high => 3,percent => 30}, ], }, 'b' => { one => {low => 1000, high => 3000, percent => 25}, two => {low => 200,high => 2000, percent => 30}, three => {low => 1000,high => 6000, percent => 40}, four => {low => 300,high => 1800, percent => 35}, five => {low => 10, high => 40, percent => 60}, six => {low => 2, high => 12, percent => 50}, seven => [ {select => 'medium',low => 1,high => 1,percent => 10}, {select => 'large',low => 1,high => 1,percent => 10}, ], }, .... more records follow );

Replies are listed 'Best First'.
Re: perl and xml
by borisz (Canon) on Aug 29, 2004 at 15:36 UTC
      I threw this together to encompass the whole system. Hopefully this provides enough information.
      #!/usr/bin/perl -w use strict; use XML::Simple; use Data::Dumper; ### BEGINNING STRUCTURE my %t_type = ( 'a' => { one => {low => 1000, high => 3000, percent => 25}, two => {low => 200,high => 2000, percent => 30}, three => {low => 1000,high => 6000, percent => 40}, four => {low => 300,high => 1800, percent => 35}, five => {low => 10, high => 40, percent => 60}, six => {low => 2, high => 12, percent => 50}, seven => [ {select => 'small',low => 3,high => 3,percent => 30}, ], }, 'b' => { one => {low => 1000, high => 3000, percent => 25}, two => {low => 200,high => 2000, percent => 30}, three => {low => 1000,high => 6000, percent => 40}, four => {low => 300,high => 1800, percent => 35}, five => {low => 10, high => 40, percent => 60}, six => {low => 2, high => 12, percent => 50}, seven => [ {select => 'medium',low => 1,high => 1,percent => 10}, {select => 'large',low => 1,high => 1,percent => 10}, ], }, ); print "\t BEFORE\n"; print "data...\n"; print "$t_type{'b'}{'seven'}[0]->{'select'} \n"; foreach my $item ( @{$t_type{b}{seven}} ) { print "... $item->{'select'}\n"; } print "\n\t AFTER\n"; print "creating file...\n"; my $xsimple = XML::Simple->new(); open(L, ">tt.xml"); print L $xsimple->XMLout(\%t_type, noattr => 1, xmldecl => '<?xml version="1.0"?>', GroupTags => { 'seven' => 'select' } ); close(L); print "reading file...\n"; %t_type = (); my $t_type = XMLin('tt.xml',GroupTags => { 'seven' => 'select' }); %t_type = \$t_type;
      %t_type = \$t_type;
      How do I dereference back into the original structure? So I can access it the same way?
      ### lets test that returned structure # yep its the same. must be a referrence problem? print "dumping structure...\n"; print Dumper($t_type);
      It appears to print the original hash except for the $VAR1 not being $t_type.
      Then try to print it the same as before but doesn't work.
      print "data...\n"; print "$t_type{'b'}{'seven'}[0]->{'select'} \n"; foreach my $item ( @{$t_type{b}{seven}} ) { print "... $item->{'select'}\n"; }
      What do I need to do?
        I think you just have to dereference t_type.
        ... my $t_type = XMLin('tt.xml',GroupTags => { 'seven' => 'select' }); %t_type = %$t_type;
        was that the question?
        Boris
Re: perl and xml
by Zaxo (Archbishop) on Aug 29, 2004 at 15:48 UTC

    You can do what you want, fairly simply, with XML::Writer. The result can be read back with XML::Parser.

    First you have to decide what kind of XML you're going to write. That means settling on a DOCTYPE declaration or a schema of some kind. What are XML tags and what are data in your hash?

    Once you have a doctype to settle that, the rest is easy. Note that since hash keys are unordered, they should be represented by XML that is also independent of order. Where order matters, arrays are used.

    I recommend Perl & XML by Ray & McIntosh (O'Reilly, 2002) to get you started. Whatever you do, make absolutely sure that what you write is truly compliant XML and not some creole of it.

    After Compline,
    Zaxo

Re: perl and xml
by gmpassos (Priest) on Aug 29, 2004 at 19:00 UTC
    Take a look into XML::Smart:
    use XML::Smart ; my $xml = new XML::Smart() ; $xml->{hash} = \%t_type ; $xml->save('hash.xml') ; my $xml_load = new XML::Smart('hash.xml') ; my %t_type_load = %{ $xml_load->tree_ok->{hash} } ; use Data::Dumper ; print Dumper( \%t_type_load ) ;

    Graciliano M. P.
    "Creativity is the expression of the liberty".

Re: perl and xml
by Eyck (Priest) on Aug 29, 2004 at 18:04 UTC
    How about XML::Mini?