in reply to XML::Simple parsing into a hash wierd behaviour

If you take a look at the source code for XML::Simple, you will find this line:

my @DefKeyAttr     = qw(name key id);

,which is a good indication of the reason that XML::Simple behaved the way you observed.

You can fix the issue by passing in the KeyAttr:

my $parser = new XML::Simple(KeyAttr=>""); my $Config = $parser->XMLin("a.xml");

That gives you:

$VAR1 = { 'type' => [ { 'report' => 'Dummy1', 'name' => 'default' }, { 'report' => 'Dummy2', 'name' => 'scenario1' } ] };

Peter (Guo) Pei

Replies are listed 'Best First'.
Re^2: XML::Simple parsing into a hash wierd behaviour
by ikegami (Patriarch) on Apr 20, 2010 at 06:31 UTC

    That's an invalid value for KeyAttr. You need to use KeyAttr=>[] or KeyAttr=>{} to override the default.

    That said, your code doesn't produce the desired output even with this fix.

      Try to run it first. I attached the result above. This is more consistent with the output when there is only one type tag. I re-read the OP, it's true that he would like to keep name as the key. Missed that.

      Peter (Guo) Pei

        Try to run it first.

        I had even though I didn't need to do so in this case. I'm quite familiar with what KeyAttr does.

        And I'm quite familiar with how to use it properly. "If you do not want folding on input or unfolding on output you must setting this option to an empty list to disable the feature." (empahsis mine). Said list is passed in an array ("KeyAttr => [ list ]").

        As whether that's the desired result, I would rather allow different opinion here.

        The OP is quite clear what he wants from the second XML snippet ("I would like to have an output like below"). Running your code produces the exact same output as without KeyAttr for this snippet.

        With default KeyAttr:

        $VAR1 = { 'type' => { 'report' => 'Dummy1', 'name' => 'default' } };

        With empty KeyAttr:

        $VAR1 = { 'type' => { 'report' => 'Dummy1', 'name' => 'default' } };

        Not only did you not fix what the OP asked for you to fix, you broke the code for the case where it used to work. The OP is quite clear that he's "getting proper result" for the first XML snippet. The result you attached shows you broke that.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: XML::Simple parsing into a hash wierd behaviour
by jthomas (Acolyte) on Apr 20, 2010 at 06:35 UTC
    Hi, With ForceArray it works. Thanks But with KeyAttr initial try says it doesnt work, even i changed the keyword "name" to something else that time also KeyAttr changes didnt help. Am i missing something

      No, you didn't miss anything. I missed one sentence in your OP, that you wanted name as the key - you said that you didn't like the output from the single element case.

      My focus was on making the multi-element case to produce a more consistent result as the single element case ;-)

      Hope this puts both of us on the same page.

      Peter (Guo) Pei

        My focus was on making the multi-element case to produce a more consistent result as the single element case

        That would be great since the OP asked for the single element case to stop "behaving differently". Unfortuantly, KeyAttr=>[] doesn't achieve that goal at all.

        Default Multiple: $tree->{type}{default}{report} Single: $tree->{type} {report} Inconsistent KeyAttr=>[] Multiple: $tree->{type}[0]{report} Single: $tree->{type} {report} Inconsistent ForceArray=>[qw( type )] Multiple: $tree->{type}{default}{report} Single: $tree->{type}{default}{report} Consistent ForceArray=>[qw( type )], KeyAttr=>[] Multiple: $tree->{type}[0]{report} Single: $tree->{type}[0]{report} Consistent
        Yes Peter... And thanks in tons for all the replies....