Hi, I'm learning perl and web application now. I have a xml data as shown below.

<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<photos page="1" pages="102" perpage="2" total="508">
<photo id="1969474222" owner="20137329@N04" secret="8ebd15f901" server="2185" farm="3" title="allen-iverson-dunks" ispublic="1" isfriend="0" isfamily="0" />
<photo id="1952424144" owner="7974492@N03" secret="a18966828b" server="2241" farm="3" title="Celtics vs. Nuggets" ispublic="1" isfriend="0" isfamily="0" />

Then I used XML:: Simple to analyze it.

use XML:: Simple;
use Data:: Dumper;

open(IN, "api.flickr.com.xml");
$xml = join("", <IN>);
$parser = new XML:: Simple(ForceArray => 1);
$xml_ref = $parser->XMLin($xml);
print Dumper($xml_ref);

The result is shown below.

$VAR1 = {
'photos' => [
'page' => '1',
'photo' => {
'1950975040' => {
'owner' => '88092548@N00',
'isfriend' => '0',
'ispublic' => '1',
'secret' => '6d25ddd8c4',
'farm' => '3',
'server' => '2047',
'title' => 'Allen Iverson',
'isfamily' => '0'
'1951626474' => {
'owner' => '8723641@N06',
'isfriend' => '0',
'ispublic' => '1',
'secret' => '72908e1302',
'farm' => '3',
'server' => '2318',
'title' => 'Ich und allen Iverson ...',
'isfamily' => '0'
'total' => '508',
'pages' => '102',
'perpage' => '2'
'stat' => 'ok'

I want to know how to access the value of each attribute(id, owner, etc). Usually, I had simple data, then I refered each value by for exp.


But with data format I'm having now, I can't access the value of each attribute.

Edited 3 Years Ago by happygeek: fixed formatting

Your output looks like a hash of array of hashes. Try this

print $xml_ref->{Photos}->[0]->{photo}->{ownner};
print $xml_ref->{Photos}->[0]->{photo}->{isfriend};
print $xml_ref->{Photos}->[0]->{photo}->{ispublic};

I tried it, but I don't see any output. No error occurs and nothing is shown in display.
Thanks anyway.

oops, looks like you need to use those numbered keys too, like: '1951626474'

print $xml_ref->{Photos}->[0]->{photo}->{1951626474}->{ownner};

I solved the problem. I add keyattr to the parser. Then the data become simpler and easy to access.
$parser = new XML:: Simple(ForceArray=>1, keyattr=>{});
Thanks anyway

This article has been dead for over six months. Start a new discussion instead.