Resolved!: see comments for solution and link to completed project(w/code)
I am writing a little script to pull data from a website that provides an API for accessing it. I post to the url with my API key and it returns some xml with the data I requested.
An example of the XML is here:
<?xml version="1.0" encoding="utf-8"?>
<get_devices_response>
<devices list="true">
<device>
<name>12345678</name>
<id>1234</id>
</device>
<device>
<name>87654321</name>
<id>8765</id>
</device>
</devices>
</get_devices_response>
I pass this to XML::Simple with KeyAttr=>[] and ForceArray=>
"device". The data structure returned (as shown by Data::Dumper) is:
$VAR1 = {
'devices' => {
'device' => [
{
'name' => '12345678',
'id' => '1234'
},
{
'name' => '87654321',
'id' => '8765'
}
],
'list' => 'true'
}
};
The problem I am having is getting the array of hashes under the key "device". I tried the following:
my $hash_ref = XMLin($XML, KeyAttr=>[] , ForceArray=>["device"]);
print "The array\n";
print Dumper($hash_ref->{devices}->{device});
print "The first element\n";
print Dumper($hash_ref->{devices}->{device}[0]);
print "The first element another way\n";
my @array=$hash_ref->{devices}->{device};
print Dumper($array[0]);
I expect this to return the entire array for the first print of Dumper() and the first element in the array for the second and third call of Dumper(). The following is the output:
The array
$VAR1 = [
{
'name' => '12345678',
'id' => '1234'
},
{
'name' => '87654321',
'id' => '8765'
}
];
The first element
$VAR1 = {
'name' => '12345678',
'id' => '1234'
};
The first element another way
$VAR1 = [
{
'name' => '12345678',
'id' => '1234'
},
{
'name' => '87654321',
'id' => '8765'
}
];
I have tried a number of ways to get a simple array of hashes that I can return from this subroutine, but all of them have either been wrong in some way shape or form. As a solution to this I would accept changes to how I call XML::Simple, using an other XML parser all together, and more, as long as I am able to see how many devices are returned, and the name/id for each one. But... I still would like to know where I am going wrong with the whole nested hashes and arrays (is it something to do with hashrefs vs hashes or arrayrefs vs arrays?). Any help would be greatly appreciated.
-Thanks
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.