Domanda

Input file :SN.xml

<?xml version='1.0'?>
<root>
<category cname='Cname1' id='c1'>
  <subcat key='3' sname='Subname1' sid='sid1'>
    <prod key='1' pname='Productname'>value1</prod>
  </subcat>
</category>
</root>

I am using these code for converting XML file to hash.

my $config = XML::Simple->new();
$config = XMLin('SN.xml');
print Dumper($config);

I'm getting below output :

'3' => {
          'sid' => 'sid1',
              'sname' => 'Subname1',
                   'prod' => {
                               'content' => 'value1',
                               'pname' => 'Productname',
                               'key' => '1'
                                                   }
        },

I am expecting to get output below, please help me to get this..

  '3' => {
             'sid' => 'sid1',
                     'sname' => 'Subname1',
                                       'prod' => {
                                                 '1' => {
                                                        'pname' => 'Productname',
                                                        'content' =>'value1'
                                                      },
                                                 }
             },

Please suggest me any other module producing this result too..

Thanks in Advance!

È stato utile?

Soluzione

my $xs = XML::Simple->new( ForceArray => 1 );
my $config = $xs->XMLin('SN.xml');
print Dumper $config;

The ForceArray option will force nested elements to be put in arrayrefs even if there is only one nested element. This allows the default KeyAttr option to kick in and recognise your 'key' attributes.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top