Help with Hashs/Arrays and XML::Simple

J

Jasper2000

Hi,

I'm trying to write a little script that does a query and which
returns XML data. That part is fine, and I thought I'd be okay with
XML::Simple, since I used it a day or two ago, with XML returned from
a different service, and I seemed to understand it fine, and it was
working. However, the XML in this new script is a bit more complex,
and I don't seem to be able to get it working.

Here is the top snippet and first complete "Item" from the XML file:

<SearchSuggestion version="2.0">
<Query xml:space="preserve">Clothes</Query>
<Section>
<Item>
<Text xml:space="preserve">Clothing</Text>
<Description xml:space="preserve">A feature of all modern human
societies....</Description>
<Url xml:space="preserve">http://someurl....</Url>
<Image source="http://someimageurl" width="50" height="33"/>
</Item>
<Item>
<Text xml:space="preserve">Clothes dryer</Text>
<Description xml:space="preserve">
....
....

My snippet of Perl script looks like this:

my $xml = new XML::Simple; # create object

my $data = $xml->XMLin("data.xml");

foreach my $record (@{$data->{Section}->{Item}})
{
print $record->{Text}, "<br />";
}


My output, however, looks like this:

HASH(0x1e35128)<br />HASH(0x1e35374)<br />HASH(0x1e35584)<br /
HASH(0x1e35788)
<br />HASH(0x1e4cfe0)<br />HASH(0x1e4d1e4)<br />HASH(0x1e4d3e8)<br />
HASH(0x1e4d5ec)<br />HASH(0x1e4d748)<br />HASH(0x1e4d8ec)<br />

I've tried various arrangements with the loop, but always seem to end
up with either nothing or something similar to the above.

Any help which can be provided would be most appreciated!

Thanks!
 
J

Jürgen Exner

Jasper2000 said:
Here is the top snippet and first complete "Item" from the XML file: [...]
<Text xml:space="preserve">Clothes dryer</Text> [...]
print $record->{Text}, "<br />";
My output, however, looks like this:

HASH(0x1e35128)<br />HASH(0x1e35374)<br />HASH(0x1e35584)<br /

Obviously $record->{Text} is a reference, in this case a reference to
HASH(0x1e35128). And this is very reasonable because the <Text> tag has
an attribute as well as a body. So you will need to dereference that
hash reference in order to get to its components.

To find out how the keys are named you could
- check the doc of XML::Simple (hopefully it is described there)
- print the keys of that hash
- use Data::Dumper do print that whole hash

jue
 
J

Jasper2000

My output, however, looks like this:
Obviously $record->{Text} is a reference, in this case a reference to
HASH(0x1e35128). And this is very reasonable because the <Text> tag has
an attribute as well as a body. So you will need to dereference that
hash reference in order to get to its components.

To find out how the keys are named you could
- check the doc of XML::Simple (hopefully it is described there)
- print the keys of that hash
- use Data::Dumper do print that whole hash

jue


Yeah, okay ... I understand what you're saying, and will try and
figure out how the attributes are being stored ... In fact, just as a
test, I deleted all the attribute tags and tested it, and it worked
fine, so, as you pointed out, it obviously is to do with that. Thanks
for the help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top