【发布时间】:2011-03-25 02:59:07
【问题描述】:
我正在使用 XML::Simple 来解析 XML 文件。输出是一个哈希。(使用 Data::Dumper)
下面给出了示例代码,XML 文件和输出。
Perl 代码:
use XML::Simple;
use Data::Dumper;
$xml = new XML::Simple;
$data = $xml->XMLin("spam.xml");
print Dumper($data);
XML 文件内容(输入到解析器)::
<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>
输出::
$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 '
}
};
我的另一个问题是:
- 有什么方法可以维持输出的顺序,就像我在输入 XML 文件中给出的一样??
【问题讨论】: