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' => {} } }; ---------------------------------------------------------------------- +---------
In reply to XML::Simple hash tree structure not consistent by riddlemethisbatman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |