I can't seem to get XML:Simple to create a hash tree the same way for different data. If there are two entries in my tree then I get named hashes of named hashes. If the XML only has one entry then the structure changes. How do I force the same structure all the time? The Perl:
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"; }
Results:
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.