You are here:  » Non Record Data


Non Record Data

Submitted by ukdave on Mon, 2006-07-17 21:52 in

There are a couple of items I need to retreive which are contained within the header section of the live data feed I'm trying to parse, they are are NUMTOTALRESULTS & NUMSHOPS. Both are vital but only need to be printed out once separate to the array produced by the myRecordHandler function. These two are not being returned at all it is as if the script is ignoring the header altogether.

How do I retrieve and print these two items?

The format I've been using is: xml|PRODUCTSEARCH/RESULTLIST/RESULT/

Example feed url:

http://export.kelkoo.co.uk/ctl/exportSearch?partner=tradedoubler&siteSearchQuery=lord+of+the+rings&logicalType=and&partnerId=96906467&catId=149201&nbresult=3

Thanks.

Submitted by support on Tue, 2006-07-18 06:17

Hi,

To extract information from higher up the XML tree you need to use a different format string that effectively makes the entire document become a single record.

You can see this in the demo script by using your example feed with the format string "xml|PRODUCTSEARCH/" (without the quotes):

Demo URL

On that demo page (will be deleted shortly) you can see that you would be able to access the information you require as follows:

$record["HEADER/NUMSHOPS"];
$record["HEADER/NUMTOTALRESULTS"];

To do this, the best way would be to create a second record handler function just for this purpose with a different name, and then use another record handler for the actual parsing, for example:

<?php
  $url 
"your_kelkoo_url";
  
$numshops 0;
  
$numtotalresults 0;
  function 
myHeaderRecordHandler($record)
  {
    global 
$numshops;
    global 
$numtotalresults;
    
$numshops $record["HEADER/NUMSHOPS"];
    
$numtotalresults $record["HEADER/NUMTOTALRESULTS"];
  }
  
MagicParser_parse($url,"myHeaderRecordHandler","xml|PRODUCTSEARCH/");
  function 
myRecordHandler($record)
  {
    
// original record handler function
  
}
  
MagicParser_parse($url,"myHeaderRecordHandler","xml|PRODUCTSEARCH/RESULTLIST/RESULT/");
?>

Please note however that this is not very efficient because it means that the feed will be requested from the server twice. If you need to do this, I would strongly recommend creating a caching system that retrieves the file and then performs the parsing from the local copy. There some information on creating a simple caching system in the following thread:

http://www.magicparser.com/node/136

Cheers,
David.

Submitted by ukdave on Tue, 2006-07-18 10:18

Thanks but there seems to be two problem with the method you described. First of all I get the following error message:

Fatal error: Cannot redeclare myrecordhandler() (previously declared in d:\sites\blablabla

Second is that even if I use the bit of code you provided by itself it still doesn't read the header.

Any idea what is going on here.

Submitted by support on Tue, 2006-07-18 19:32

Hi,

Regarding the "Cannot redeclare myrecordhandler()" error; that would imply that the second record handler provided for reading the header (or vice versa) didn't have a different name - you can only have one function called myRecordHandler().

The standalone code should work OK, although note that I didn't include the require("MagicParser.php") line. Here's the full version below including code to print the values.

You can also see it running on this server:
http://www.magicparser.com/examples/kelkoo.php

<?php
  
require("MagicParser.php");
  
$url "http://export.kelkoo.co.uk/ctl/exportSearch?partner=tradedoubler&siteSearchQuery=lord+of+the+rings&logicalType=and&partnerId=96906467&catId=149201&nbresult=3";
  
$numshops 0;
  
$numtotalresults 0;
  function 
myHeaderRecordHandler($record)
  {
    global 
$numshops;
    global 
$numtotalresults;
    
$numshops $record["HEADER/NUMSHOPS"];
    
$numtotalresults $record["HEADER/NUMTOTALRESULTS"];
  }
  
MagicParser_parse($url,"myHeaderRecordHandler","xml|PRODUCTSEARCH/");
  print 
"<p>Num Shops: ".$numshops."</p>";
  print 
"<p>Num Total Records: ".$numtotalresults."</p>";
?>

Hope this helps,
Cheers,
David.

Submitted by ukdave on Tue, 2006-07-18 22:03

Thanks for that Dave, it worked a treat. I'm going to follow your suggestion for caching next.

Thanks again!