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

Hi, I have just started to study XML::Study module. below is my script.

use warnings; #use strict; use XML::Simple; use Data::Dumper; my $xml = XML::Simple->new(); my $data = $xml->XMLin("data1.xml"); print Dumper($data);

and here is my xml file.

<?xml version='1.0'?> <employees> <employee> <name>John Doe</name> <age>43</age> <sex>M</sex> <department>Operations</department> </employee> <employee> <name>Jane Doe</name> <age>31</age> <sex>F</sex> <department>Accounts</department> </employee> </employees>

output what tutorials are getting (which seems correct) is

$data = { 'employee' => [ { 'department' => 'Operations', 'name' => 'John Doe', 'sex' => 'M', 'age' => '43' }, { 'department' => 'Accounts', 'name' => 'Jane Doe', 'sex' => 'F', 'age' => '31' } ] };

& what I am getting is

$VAR1 = { 'employee' => { 'John Doe' => { 'department' => 'Operations', 'age' => '43', 'sex' => 'M' }, 'Jane Doe' => { 'department' => 'Accounts', 'age' => '31', 'sex' => 'F' } } };

why this mismatch..please help.. I installed module using cpan command prompt method...I am not root just a user.. and working on ubuntu...thanx

Replies are listed 'Best First'.
Re: XML::Simple Not getting the proper output.
by poj (Abbot) on Jun 13, 2014 at 07:52 UTC
    why this mismatch ?

    Because name is one of the default values for the KeyAttr option. See here KeyAttr


    Note 1: The default value for 'KeyAttr' is ['name', 'key', 'id']. If you do not want folding on input or unfolding on output you must set this option to an empty list to disable the feature.
    poj

      greaatttt...It works...thanks a lot for help..

Re: XML::Simple Not getting the proper output. ( xml2XMLRules
by Anonymous Monk on Jun 13, 2014 at 06:28 UTC