riddlemethisbatman has asked for the wisdom of the Perl Monks concerning the following question:
Results:use strict; use Data::Dumper; #require XML::Simple;# qw(:strict); use XML::Simple;# qw(:strict); # # I've got two xml files. The 1st contains 1 project the 2nd contains +2. # I need XML::Simple to create the same data structure every time. # How do I do this? # # Creates $tree->{project}{name} equal to 'PlayGround'. # But I don't want that. # I want $tree->{project}{'PlayGround'}. # my $xmldata1=<<EOF; <?xml version="1.0"?> <main> <project name="PlayGround"> <state name="Development"> </state> <state name="Emergency"> </state> </project> </main> EOF &xml1($xmldata1); # # I like how this structure is created. # How do I get this structure all the time? # my $xmldata2=<<EOF; <?xml version="1.0"?> <main> <project name="PlayGround"> <state name="Development"> </state> <state name="Emergency"> </state> </project> <project name="GymClass"> <state name="Development"> </state> <state name="Emergency"> </state> </project> </main> EOF &xml1($xmldata2); # --------------- show xml data. sub xml1 { my $data=shift; print "data\n$data\n"; my $simple = XML::Simple->new( ); my $tree = $simple->XMLin( $data #,forcearray=>0,KeyAttr => [ ] # creates array for 2nd +file. #,forcearray=>0 # no affect #,forcearray=>0 # no affect #,forcearray=>0,KeyAttr => [ ],ValueAttr => { project => 'name' } #,VarAttr => 'name' # no affect #,ValueAttr => { project => 'name' } # no affect #,ValueAttr => [ 'name' ] # no affect #,KeyAttr => { project => "+name" } # no affect #,KeyAttr => { project => 'name' } # no affect #,ForceContent => 1 # no affect ); print "0", Dumper( $tree ); print "1", Dumper( $tree->{project}{name} ); # don't wan +t this. print "2", Dumper( $tree->{project}{'PlayGround'} ) ; # want this + all the time. print "-" x 79, "\n"; }
E:\bin\XML>xmlqna.pl data <?xml version="1.0"?> <main> <project name="PlayGround"> <state name="Development"> </state> <state name="Emergency"> </state> </project> </main> 0$VAR1 = { 'project' => { 'name' => 'PlayGround', 'state' => { 'Emergency' => {}, 'Development' => {} } } }; 1$VAR1 = 'PlayGround'; 2$VAR1 = undef; ---------------------------------------------------------------------- +--------- data <?xml version="1.0"?> <main> <project name="PlayGround"> <state name="Development"> </state> <state name="Emergency"> </state> </project> <project name="GymClass"> <state name="Development"> </state> <state name="Emergency"> </state> </project> </main> 0$VAR1 = { 'project' => { 'PlayGround' => { 'state' => { 'Emergency' => {}, 'Development' => {} } }, 'GymClass' => { 'state' => { 'Emergency' => {}, 'Development' => {} } } } }; 1$VAR1 = undef; 2$VAR1 = { 'state' => { 'Emergency' => {}, 'Development' => {} } }; ---------------------------------------------------------------------- +---------
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::Simple hash tree structure not consistent
by ferreira (Chaplain) on Mar 29, 2007 at 18:32 UTC | |
|
Re: XML::Simple hash tree structure not consistent
by osunderdog (Deacon) on Mar 29, 2007 at 18:32 UTC | |
by riddlemethisbatman (Initiate) on Mar 29, 2007 at 19:44 UTC | |
by riddlemethisbatman (Initiate) on Mar 31, 2007 at 16:08 UTC | |
|
Re: XML::Simple hash tree structure not consistent
by philcrow (Priest) on Mar 29, 2007 at 18:34 UTC |