Gossamer Forum
Home : General : Perl Programming :

XML::Simple...

Quote Reply
XML::Simple...
Man ... I normally manage to get XML::Simple working fine on XML feeds.. but this one I just can't figure out :(

The structure is as follows;

Code:
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name><![CDATA[OLYMPUS MJU 410 ZOOM DIGITAL CAMERA]]></name>
<description><![CDATA[ OLYMPUS MJU 410 ZOOM DIGITAL CAMERA ]]></description>
<imageUrl><![CDATA[ http://www.site.com/images/products/t_OLYMJUD410Z.gif]]></imageUrl>
<productUrl><![CDATA[http://www.site.com/click?a=322582&p=17211&prod=5908504]]></productUrl>
<price>279.9</price>
<currency>GBP</currency>
<TDProductId>5908504</TDProductId>
<TDCategories>
<TDCategory>
<name><![CDATA[Digital Compact Cameras]]></name>
</TDCategory>
</TDCategories>
<fields></fields>
</product>

<product>
<name><![CDATA[OLYMPUS MJU 410 ZOOM DIGITAL CAMERA]]></name>
<description><![CDATA[ OLYMPUS MJU 410 ZOOM DIGITAL CAMERA ]]></description>
<imageUrl><![CDATA[ http://www.site.com/images/products/t_OLYMJUD410Z.gif]]></imageUrl>
<productUrl><![CDATA[http://www.site.com/click?a=322582&p=17211&prod=5908504]]></productUrl>
<price>279.9</price>
<currency>GBP</currency>
<TDProductId>5908504</TDProductId>
<TDCategories>
<TDCategory>
<name><![CDATA[Digital Compact Cameras]]></name>
</TDCategory>
</TDCategories>
<fields></fields>
</product>

</products>

I've tried so many ways.. but none of them yeild a value :(

Code:
my $ref = XMLin($xml);
print "TEST: " . $ref->{products}->{product}->{name}->[0];
print "TEST: " . $ref->{products}->[0]->{product}->{name};
print "TEST: " . $ref->{products}->{product}[0]->{name};

...etc

Even this gives no results;

Code:
Dumper($ref->{products})

Quote:
TEST: $VAR1 = undef;

Am I just being really stupid, and missing something simple? This is sending me mad Pirate

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] XML::Simple... In reply to
Hey Andy,

This works for me:
Code:
#!/usr/bin/perl
use warnings;
use strict;
use XML::Simple;
use Data::Dumper;

my $xml = q~<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name><![CDATA[OLYMPUS MJU 410 ZOOM DIGITAL CAMERA]]></name>
<description><![CDATA[ OLYMPUS MJU 410 ZOOM DIGITAL CAMERA ]]></description>
<imageUrl><![CDATA[ http://www.site.com/images/products/t_OLYMJUD410Z.gif]]></imageUrl>
<productUrl><![CDATA[http://www.site.com/click?a=322582&p=17211&prod=5908504]]></productUrl>
<price>279.9</price>
<currency>GBP</currency>
<TDProductId>5908504</TDProductId>
<TDCategories>
<TDCategory>
<name><![CDATA[Digital Compact Cameras]]></name>
</TDCategory>
</TDCategories>
<fields></fields>
</product>
<product>
<name><![CDATA[OLYMPUS MJU 410 ZOOM DIGITAL CAMERA]]></name>
<description><![CDATA[ OLYMPUS MJU 410 ZOOM DIGITAL CAMERA ]]></description>
<imageUrl><![CDATA[ http://www.site.com/images/products/t_OLYMJUD410Z.gif]]></imageUrl>
<productUrl><![CDATA[http://www.site.com/click?a=322582&p=17211&prod=5908504]]></productUrl>
<price>279.9</price>
<currency>GBP</currency>
<TDProductId>5908504</TDProductId>
<TDCategories>
<TDCategory>
<name><![CDATA[Digital Compact Cameras]]></name>
</TDCategory>
</TDCategories>
<fields></fields>
</product>
</products>
~;

print "content-type: text/html\n\n";
print '<pre>', $/;
print 'XML::Simple version: ', $XML::Simple::VERSION, $/;
print Dumper(XMLin($xml, KeepRoot => 1, KeyAttr => 'Product'));
print '</pre>', $/;

and produces:
Code:
XML::Sinmple version: 2.09
$VAR1 = {
'products' => {
'product' => [
{
'name' => 'OLYMPUS MJU 410 ZOOM DIGITAL CAMERA',
'TDCategories' => {
'TDCategory' => {
'name' => 'Digital Compact Cameras'
}
},
'description' => ' OLYMPUS MJU 410 ZOOM DIGITAL CAMERA ',
'currency' => 'GBP',
'imageUrl' => ' http://www.site.com/images/products/t_OLYMJUD410Z.gif',
'fields' => {},
'TDProductId' => '5908504',
'productUrl' => 'http://www.site.com/click?a=322582&p=17211&prod=5908504',
'price' => '279.9'
},
{
'name' => 'OLYMPUS MJU 410 ZOOM DIGITAL CAMERA',
'TDCategories' => {
'TDCategory' => {
'name' => 'Digital Compact Cameras'
}
},
'description' => ' OLYMPUS MJU 410 ZOOM DIGITAL CAMERA ',
'currency' => 'GBP',
'imageUrl' => ' http://www.site.com/images/products/t_OLYMJUD410Z.gif',
'fields' => {},
'TDProductId' => '5908504',
'productUrl' => 'http://www.site.com/click?a=322582&p=17211&prod=5908504',
'price' => '279.9'
}
]
}
};

~Charlie

[edit]To answer your question, Products was the root which isn't part of the hash by default. If you add the KeepRoot => 1 option it keeps Products in there. I also added KeyAttr => 'Product' so that both products display; they are identical so they would overwrite each other inthe hash otherwise.[/edit]

Last edited by:

Chaz: Jul 12, 2004, 9:54 AM
Quote Reply
Re: [Chaz] XML::Simple... In reply to
Thanks for the reply Charlie. I'm guessing part of my problem, is that I'm running 2.03 of XML::Simple, and not 2.09, which you seem to be. I've sent an email to GT, to see if they can get the module updated for me :)

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] XML::Simple... In reply to
That could be the problem. I think the latest version is 2.12 but if they do upgrade CPAN will grab the latest anyhow. Did you try it with the two options I mentioned? That may be all you need.

~Charlie
Quote Reply
Re: [Chaz] XML::Simple... In reply to
Yeah, they should have it upgraded by the morning :)

I tried the options you suggested, but it doesn't look like they are valid in my version, as they were spewing up "invalid option" errors all over the place :(

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates