Help with Hashs/Arrays and XML::Simple

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


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>
<Text xml:space="preserve">Clothing</Text>
<Description xml:space="preserve">A feature of all modern human
<Url xml:space="preserve">http://someurl....</Url>
<Image source="http://someimageurl" width="50" height="33"/>
<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 (@->})
print $record->, "<br />";

My output, however, looks like this:

HASH(0x1e35128)<br />HASH(0x1e35374)<br />HASH(0x1e35584)<br /
<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!


Re: Help with Hashs/Arrays and XML::Simple

Quoted text here. Click to load it
Quoted text here. Click to load it
Quoted text here. Click to load it

Obviously $record-> 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


Re: Help with Hashs/Arrays and XML::Simple

Quoted text here. Click to load it

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!

Site Timeline