tags:

views:

81

answers:

3

Hi,

I am using XML::Simple to parse a XML file. The output is a hash.(using Data::Dumper)

Sample code , XML file are given below with output.

Perl code:

use XML::Simple;
use Data::Dumper;

$xml = new XML::Simple;

$data = $xml->XMLin("spam.xml");

print Dumper($data);

XML file contents (input to parser)::

<Attach_request>
    <Protocol_discriminator>
        <name> Protocol_discriminator </name>
        <attribute> Mandatory </attribute>
        <type> nibble </type>
        <value> 7 </value>
        <min> 0 </min>
        <max> F </max>
    </Protocol_discriminator>
    <Security_header>
        <name> Security_header </name>
        <attribute> Mandatory </attribute>
        <type> nibble </type>
        <value> 7 </value>
        <min> 0 </min>
        <max> F </max>
    </Security_header>
    <Security_header1>
        <name> Security_header </name>
        <attribute> Mandatory </attribute>
        <type> nibble </type>
        <value> 7 </value>
        <min> 0 </min>
        <max> F </max>
    </Security_header1>
    <Security_header2>
        <name> Security_header </name>
        <attribute> Mandatory </attribute>
        <type> nibble </type>
        <value> 7 </value>
        <min> 0 </min>
        <max> F </max>
    </Security_header2>
    <Security_header3>
        <name> Security_header </name>
        <attribute> Mandatory </attribute>
        <type> nibble </type>
        <value> 7 </value>
        <min> 0 </min>
        <max> F </max>
    </Security_header3>
</Attach_request>

Output::

$VAR1 = {
          'Security_header3' => {
                                'attribute' => ' Mandatory ',
                                'min' => ' 0 ',
                                'value' => ' 7 ',
                                'max' => ' F ',
                                'name' => ' Security_header ',
                                'type' => ' nibble '
                              },
          'Protocol_discriminator' => {
                                      'attribute' => ' Mandatory ',
                                      'min' => ' 0 ',
                                      'value' => ' 7 ',
                                      'max' => ' F ',
                                      'name' => ' Protocol_discriminator ',
                                      'type' => ' nibble '
                                    },
          'Security_header2' => {
                                'attribute' => ' Mandatory ',
                                'min' => ' 0 ',
                                'value' => ' 7 ',
                                'max' => ' F ',
                                'name' => ' Security_header ',
                                'type' => ' nibble '
                              },
          'Security_header' => {
                               'attribute' => ' Mandatory ',
                               'min' => ' 0 ',
                               'value' => ' 7 ',
                               'max' => ' F ',
                               'name' => ' Security_header ',
                               'type' => ' nibble '
                             },
          'Security_header1' => {
                                'attribute' => ' Mandatory ',
                                'min' => ' 0 ',
                                'value' => ' 7 ',
                                'max' => ' F ',
                                'name' => ' Security_header ',
                                'type' => ' nibble '
                              }
        };

My other question is::

  1. Is there any way i can manintain the order of the output, the same i gave in the input XML file ??
A: 

I doubt if it is possible with XML::Simple (perhaps you'd have to sort hash keys in some way). You can also use XML::DOM and enumerate through child nodes.

hlynur
+6  A: 

If you see the documentation of XML::Simple, it states that

XML::Simple is able to present a simple API because it makes some assumptions on your behalf. These include:

  1. You're not interested in text content consisting only of whitespace

  2. You don't mind that when things get slurped into a hash the order is lost

  3. You don't want fine-grained control of the formatting of generated XML

  4. You would never use a hash key that was not a legal XML element name

  5. You don't need help converting between different encodings.

For tree-based parsing, you could choose between the 'Perlish' approach of XML::Twig and more standards based DOM implementations - preferably one with XPath support.

see also 'Perl-XML FAQ' for more detail.

Nikhil Jain
A: 

Some of the ordering would be retained if you turned off the KeyAttr option. If you do decide to stick with XML::Simple and mess around with different settings then you really ought to have a quick read through this article:

A better approach would be to ditch XML::Simple altogether as advised in this article:

Grant McLean