I have an XML that I need to convert to a hash in a specific format that requires some nodes to be in an array. I've tried XML::Simple but can't get rid of one xml node level.
#!/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;
Desired output
'images' => [
{
'type' => 'primary',
'width' => 600,
'resource_url' => '',
'uri150' => '',
'height' => 511,
'uri' => ''
},
{
'width' => 600,
'type' => 'secondary',
'resource_url' => '',
'uri150' => '',
'uri' => '',
'height' => 519
}, etc...
What I'm getting with my sample code is
$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...
How do I get rid of
'image' => [
and have
'images' => [
contain all the hashes ?
Thanks; George