【发布时间】:2011-04-20 21:56:52
【问题描述】:
我正在使用 XML::Simple 来解析一个 XML 文件,然后我想用它来编写一个非常特定格式的输出文件。因此,输出顺序很重要。
据我了解,当 XML 转换为 perl hashref 时,顺序会丢失(因为 perl 哈希没有顺序)。但是当 XML::Simple 使用数组时呢?
例如:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>10.0</price>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.99</price>
</cd>
<cd>
<title>Hello</title>
<artist>Say Hello</artist>
<price>0001</price>
</cd>
</catalog>
给了我们一个类似的数据结构:
$VAR1 = {
'cd' => [
{
'artist' => 'Bonnie Tyler',
'price' => '10.0',
'title' => 'Hide your heart'
},
{
'artist' => 'Dolly Parton',
'price' => '9.99',
'title' => 'Greatest Hits'
},
{
'artist' => 'Say Hello',
'price' => '0001',
'title' => 'Hello'
}
]
};
这 3 个“cd”结构被插入到一个数组中,那么它们的顺序是否总是与它们在输入文件中的顺序相同?
【问题讨论】:
-
你尝试的时候发生了什么?
-
每次我都尝试过,但我需要知道这是否有保证。
标签: xml perl xml-simple