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:
- Is there any way i can manintain the order of the output, the same i gave in the input XML file ??
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:
You’re not interested in text content consisting only of whitespace
You don’t mind that when things get slurped into a hash the order is lost
You don’t want fine-grained control of the formatting of generated XML
You would never use a hash key that was not a legal XML element name
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.