Reputation: 993
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:
Upvotes: 0
Views: 657
Reputation: 6998
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:
Edit (9 years later): I've subsequently created a tutorial site called Perl XML::LibXML by Example that introduces the XML::LibXML library and XPath using lots of examples. XML::LibXML is a much better module for working with XML than XML::Simple and using it is generally simpler due to the power of XPath.
Upvotes: 1
Reputation: 8332
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.
Upvotes: 6
Reputation: 596
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.
Upvotes: 0