Perl,使用 XML::Simple 解析 XML 但存在问题
我正在 Perl 中解析 xml 文件,并且一切似乎都可以很好地解决一个问题。我有具有相同架构的文件,但它们从解析器返回不同类型的数据。这是一个简化的示例:
<tests>
<test>
<data1>Hi</data1>
<data2>Hello</data2>
</test>
<test>
<data1>Hi2</data1>
<data2>Hello2</data2>
</test>
</tests>
在转储中,这将返回以下内容:(请注意 test 是两个哈希值的数组)
$VAR1 = {
'test' => [
{
'data2' => 'Hello',
'data1' => 'Hi'
},
{
'data2' => 'Hello2',
'data1' => 'Hi2'
}
]
};
现在,对于一组类似的数据,但只有一个“测试”实体,如下所示:
<tests>
<test>
<data1>Hi</data1>
<data2>Hello</data2>
</test>
</tests>
这将返回类似的结果数据,除了测试实体不再是一个数组,而是一个单一的散列:
$VAR1 = {
'test' => {
'data2' => 'Hello',
'data1' => 'Hi'
}
};
我的困境是我的代码需要一个数组,因为这是常态。但是,当只有一个实体时,它会返回该实体的哈希值。我的问题是,如何处理哈希实体,就像它是一个数组一样。或者测试一下?
现在,我用于检索数组的代码如下:
foreach $test (@{$data->{'tests'}->{'test'}})
{
do something with $test
}
但是使用哈希时,它会给出错误“不是数组引用”。我希望这是足够的细节!谢谢!!!
I am parsing xml files in Perl and everything seems to work great with one issue. I have files with the same schema, but they are returning different types of data from the parser. Here is a simplified example:
<tests>
<test>
<data1>Hi</data1>
<data2>Hello</data2>
</test>
<test>
<data1>Hi2</data1>
<data2>Hello2</data2>
</test>
</tests>
In a dump, this returns the following: (Take note of test being an array of two hashes)
$VAR1 = {
'test' => [
{
'data2' => 'Hello',
'data1' => 'Hi'
},
{
'data2' => 'Hello2',
'data1' => 'Hi2'
}
]
};
Now, for a similar set of data, but with only one 'test' entity like so:
<tests>
<test>
<data1>Hi</data1>
<data2>Hello</data2>
</test>
</tests>
This returns similar data, EXCEPT the test entity is no longer an array, but a singular hash:
$VAR1 = {
'test' => {
'data2' => 'Hello',
'data1' => 'Hi'
}
};
My dilemma is that my code expects an array there, as that is the norm. But on the slim chance when there is only one entity, it will return a hash for that entity. My question is, how can I handle the hash entity as though it were an array. Or test for it?
Right now my code for retrieving the array is like this:
foreach $test (@{$data->{'tests'}->{'test'}})
{
do something with $test
}
But with the hash, it gives an error "Not an ARRAY reference". I hope this is enough detail! Thanks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许
ForceArray
选项的替代形式正是您想要的?所以我可能会尝试:
Perhaps the alternate form of the
ForceArray
option is what you want?So I might try:
XML::Simple
XML::Simple
您需要使用哈希符号“%”来取消引用哈希。
You need to dereference the hash by using the hash sigil: '%'.
虽然看起来您可以使 XML 解析器的行为更加一致,但让您的代码处理变体输出也不困难。
Perl 内置函数“ref”可用于确定引用引用的对象类型。
您的原始代码是
(而不是编写 $data->{'tests'}->{'test'} ,我倾向于使用更紧凑的 $$data{tests}{test} ,所以我将使用在我的例子中。)
我们可以检查引用类型并使用它来将所有可能性推入一个数组中,所以
Whilst it appears that you could get the XML parser to behave more consistently, it would also not be difficult to make your code work on the variant output.
The Perl built-in function "ref" can be used to determine what type of object a reference refers to.
Your original code goes
(Rather than write $data->{'tests'}->{'test'}, I would tend to use the more compact $$data{tests}{test} , so I'll use that in my example.)
We could check the reference type and use that to push all possibilities into an array, so