Gossamer Forum
Home : General : Perl Programming :

XML::Simple problems!

Quote Reply
XML::Simple problems!
Eugh.. got a REALLY annoying XML::Simple problem. Basically, if only one results is found, it just crashes the script (doesn't die, it just gives a blank page!).... can anyone see a problem here? The bit in red has had to be set to 1, cos if I set it to > 0, it just gives me a blank page.

It seems to be down to the first variable being accessed, but I can't work out why. Using Data::Dumper prints out the first entry, which seems to all be ok. As soon as I try to refrence it (even without the loop), with something like;

Code:
print "<BR>Listing: " . $ref->{ResultSet}->{Listing}->[0]->{title} . "<BR>";

.. it doesn't print it out (not even the Listing: path) ... just gives me a blank page.

This is driving me mad Mad

The routine is as follows ($keyword holds the corect value), and is called with;

Code:
$results = &overture_run($keyword);

..code is;

Code:
# this is the routine that will pass back the overture results...
sub overture_run {

my $keyword = $_[0];
my $Limit = 10;

my $page = get("http://xml.uk.feed.overture.com/d/search/p/feed/xml/uk/ctxt/?mkt=uk&maxCount=11&adultFilter=clean&Partner=feed_xml_uk_ctxt&Keywords=$keyword");
if ($page !~ /Listing rank/) { return 'none found'; }

# parse the page...
my $ref = XMLin($page);

# grab the number of results we have...
my $track_hits = $ref->{ResultSet}->{numResults};

# now lets go through, and create the HTML for it...
my $html_links;
if ($track_hits > 1) {
for (my $count = 1; $count <= $track_hits; $count++) {

# get the variables all setup...
my $Description = $ref->{ResultSet}->{Listing}->[$count]->{description};
my $Title = $ref->{ResultSet}->{Listing}->[$count]->{title};
my $URI = $ref->{ResultSet}->{Listing}->[$count]->{ClickUrl}->{content};
my $ClickURL = $ref->{ResultSet}->{Listing}->[$count]->{siteHost};

# put the links into HTML format...
$html_links .= Links::SiteHTML::display('overture_link', { Description => $Description, Title => $Title, URL => $URI, ShowURL => $ClickURL });

}
}

# now lets send it back to the template....
$html_links ? return Links::SiteHTML::display('overture_search', { results => $html_links } ) : return '';

}


PLEASE someone help me here. I'm going out of my mind Frown

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] XML::Simple problems! In reply to
BTW, just upgraded to 2.09 ... and still no joys (was running 2.03).

Anyone got any ideas? Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] XML::Simple problems! In reply to
ARGHHHHH! Still driving me mad :(

After the get() and XMLIn() codes, I have now put the following.. to verify the data is there;

Code:
use Data::Dumper;
print Dumper($ref) . "\n";
print $ref->{ResultSet}->{Listing}->[0]->{title} . "\n";
exit;

When I then run the script, I get this;

Code:
[root@www cgi-bin]# perl TranslateXmlToHtml.pl
Number: 1$VAR1 = {
'xsi:noNamespaceSchemaLocation' => 'http://dtd.overture.com/schema/affiliate/2.8/OvertureSearchResults.xsd',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'ResultSet' => {
'numResults' => '1',
'id' => 'searchResults',
'Listing' => {
'siteHost' => 'www.kelkoo.co.uk',
'description' => 'Kelkoo is the unbiased shopping search engine that compares prices from hundreds of UK shopping sites to help you find great deals on videos and DVDs available to buy online.',
'title' => 'Great Film Deals at Kelkoo: Escort',
'ClickUrl' => {
'content' => 'http://www6.overture.com/d/sr/?xargs=02u3hs9yoakFVuyzCDRRAmvxJeMD%2BFj4VVKgBGq9RNZG1RDOAowXFptL6%2Fbg51VZGkjq7uZ3fIb%2Fm31g9DZuClgs7fMFfYNT%2BtvBDmzjLhuMH5YcvPRobaSrSDx1d%2BljlDLy0w3V56jjUwkjEJwHF2dh1ojiin%2B5PnUgOYWXAcfzKdyWcAvRAePOV%2F6BdC%2B2Tl5G91ZHU7y3fFJLXMBsNizIdmrDQnf6MMCtqUghggd6MowY1S6MDX4IWFLQ8jdVhr%2BewONqXnKz%2Bzexl%2FLPJhxie0KynGWOwzJEOga0cUhZxMOFIwg1ITfqztsmDka4k0POLLiV5dee65TL3PA%2FD%2B3o3UwK2VaxZxjBZdkmyKmGWhqET5dXMkEAGlkonigPCAG%2BSyAHJ%2F',
'type' => 'body'
},
'rank' => '1'
}
}
};

Not an ARRAY reference at TranslateXmlToHtml.pl line 34.
[root@www cgi-bin]#

As you can clearly see, the data is there! Why oh why am I getting this ARRAY reference error? Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] XML::Simple problems! In reply to
With this:

Code:
if ($track_hits > 1) {
for (my $count = 1; $count <= $track_hits; $count++) {

It's not going to run if you only have 1 result. Also, you should set $count to 0 in order to access the first element of the array.

I would think that you can do away with the if statement altogether though:

Code:
for (my $count = 0; $count < $track_hits; $count++) {

It wont enter the loop if there are no results.

Regards,
Charlie
Quote Reply
Re: [Andy] XML::Simple problems! In reply to
Listing is a hasref there. It looks like maybe when Listing is returned as a single result it's a hasref, not an arrayref? Try something that fetches two (or more) rows and post the results of that.

~Charlie
Quote Reply
Re: [Chaz] XML::Simple problems! In reply to
Yeah.. if there is more thanone result in the XML.. it works fine....just doesn't work when there is one? How would I get around this? Maybe use;

$ref->{ResultSet}->{Listing}->{title}

... if only one result?

Thanks for the replys...

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] XML::Simple problems! In reply to
Nah, that sounds like too much work to me :) Have a look at this first: http://search.cpan.org/...;_1_#_in_-_important

~Charlie

Last edited by:

Chaz: Sep 18, 2003, 6:49 AM
Quote Reply
Re: [Chaz] XML::Simple problems! In reply to
LOL... lasy Wink

Didn't take that much work. Now using this;

Code:
my $track_hits = $ref->{ResultSet}->{numResults};

if (!$track_hits) { return; }

# now lets go through, and create the HTML for it...
my $html_links;
if ($track_hits > 1) {
for (my $count = 1; $count <= $track_hits; $count++) {
# get the variables all setup...
print $ref->{ResultSet}->{Listing}->[$count]->{title} . "\n";

}
} else {
# get the variables all setup...
print $ref->{ResultSet}->{Listing}->{title} . "\n";
}

Couldn't figure out that forcearray stuff...LOL

Thanks for your help Charlie.... you have saved the rest of my hair :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] XML::Simple problems! In reply to
No worries. Glad you got it working.

~Charlie
Quote Reply
Re: [Andy] XML::Simple problems! In reply to
I remember sending an email to that module's author a long while ago begging him to turn ForceArray on by default. You'll be amazed at how many people stumble on that one.

Although, I guess you should understand why you're code fails before turning it on, which is a valid argument.

- wil
Quote Reply
Re: [Wil] XML::Simple problems! In reply to
Yeah...I can see why its failing *now*... but its a mega pain in the arse. Not even sure why its off by default? Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!