Вопрос

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!

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top