【发布时间】:2020-05-11 03:56:47
【问题描述】:
我有一个 XML,我需要将其转换为特定格式的哈希,该格式需要一些节点位于数组中。我试过 XML::Simple 但无法摆脱一个 xml 节点级别。
#!/usr/bin/perl
use Data::Dumper::Simple;
use XML::Simple;
use warnings;
use strict;
my $xml = <<'XML';
<?xml version="1.0"?>
<release id="9999" status="Accepted">
<images>
<image height="511" type="primary" uri="" uri150="" width="600"/>
<image height="519" type="secondary" uri="" uri150="" width="600"/>
<image height="521" type="secondary" uri="" uri150="" width="600"/>
<image height="217" type="secondary" uri="" uri150="" width="500"/>
<image height="597" type="secondary" uri="" uri150="" width="600"/>
<image height="89" type="secondary" uri="" uri150="" width="600"/>
</images>
<artists>
<artist>
<id>45</id>
<name>Aphex Twin</name>
<anv/>
<join/>
<role/>
<tracks/>
</artist>
</artists>
</release>
XML
my $xml_hash = XMLin($xml, ForceArray => qr{image}x );
print Dumper $xml_hash;
期望的输出
'images' => [
{
'type' => 'primary',
'width' => 600,
'resource_url' => '',
'uri150' => '',
'height' => 511,
'uri' => ''
},
{
'width' => 600,
'type' => 'secondary',
'resource_url' => '',
'uri150' => '',
'uri' => '',
'height' => 519
}, etc...
我的示例代码得到的是
$xml_hash = {
'images' => [
{
'image' => [
{
'uri150' => '',
'type' => 'primary',
'uri' => '',
'height' => '511',
'width' => '600'
},
{
'type' => 'secondary',
'uri150' => '',
'uri' => '',
'height' => '519',
'width' => '600'
},
{
'uri' => '',
'height' => '521',
'width' => '600',
'type' => 'secondary',
'uri150' => ''
},
etc...
如何摆脱
'图像' => [
并且拥有
'图片' => [
包含所有哈希?
谢谢;乔治
【问题讨论】: