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

This has me confounded. Any help would be greatly appreciated. I have a blessed hash: $data
print Data::Dumper($data)
gives me this:
///////////////////////////////////////////////////////////////// bless( { 'namespace' => { 'jmm' => { 'types' => {}, 'handles' => { 'shader' = +> { + 'ui' => { + 'synthetic' => '', + 'desc' => 'shader name', + 'order' => 0, + 'name' => 'shader' + }, + 'order' => 0, + 'type' => 'string', + 'default' => undef, + 'datatype' => 'string', + 'semantic' => '' + } } } }, 'name' => 'jmm', 'path' => 'jmm_mtl' }, 'material' ); ///////////////////////////////////////////////////////////// XML::Simple->new(XMLDecl=>1, NoSort=>1, KeepRoot=>1, NoAttr=>1, RootName=>ref($self) );
gives me this:
/////////////////////////////////////////////////////////////// <?xml version=\'1.0\' standalone=\'yes\'?> <material> <namespace> <types></types> <handles> <order>0</order> <ui> <synthetic></synthetic> <desc>shader name</desc> <order>0</order> <name>shader</name> </ui> <name>shader</name> <default></default> <type>string</type> <semantic></semantic> <datatype>string</datatype> </handles> <name>jmm</name> </namespace> <name>jmm</name> <path>jmm_mtl</path> </material> ///////////////////////////////////////////////////////////////////
Notice the placement of the singleton entry in both namespace hash and the handles hash namely 'jmm' and 'shader' repectively into a "name" tag. So when I load up the XML data:
/////////////////////////////////////////////////////////////////// my $xml = XML::Simple->new(XMLDecl=>1, NoSort=>1, KeepRoot=>1, NoAttr=>1, RootName=>"mtl" ); ///////////////////////////////////////////////////////////////////
I get this:
/////////////////////////////////////////////////////////////////// bless( { 'namespace' => { 'handles' => { 'ui' => { 'order' => '0', 'desc' => 'shader + name', 'name' => 'shader +' }, 'order' => '0', 'name' => 'shader', 'type' => 'string', 'datatype' => 'string' }, 'name' => 'jmm' }, 'name' => 'jmm', 'path' => 'jmm_mtl' }, 'material' ); ///////////////////////////////////////////////////////////////////
This does not happen if there is more than one key in either of the nested hashes. Two or more keys in the nexted hash returns the identical structure I had when I started. Have I found a bug? If not how do I force the module not to exhibit this behavior or must I accept this reality "as it is" ? Jean-Michel

Replies are listed 'Best First'.
Re: XML::Simple nested hash bug?
by ikegami (Patriarch) on May 20, 2007 at 05:26 UTC

    Nodes contains *array* of nodes. If you wish to be unambiguous,

    { handles => [ { jmm => ... }, ] },

    I never really understood why XML::Simple does so much mucking around. If it changes your data structure, how can you find the element you want?!

Re: XML::Simple nested hash bug?
by planetscape (Chancellor) on May 20, 2007 at 06:28 UTC

    Use XML::Twig. Super Searching for "XML::Twig XML::Simple" would doubtless turn up many comparisons of features and reasons to prefer one over the other.

    HTH,

    planetscape