Affiliate Marketing
Forum Search

Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)  
Old 29-08-07
Registered User
 
Join Date: Aug 2003
Posts: 218
Thanks: 1
Thanked 0 Times in 0 Posts
Andy is an unknown quantity at this point
  Parsing Affiliatewindow XML Feed 1.2

Hi,

Can anyone help me in writing PHP code for the 1.2 version of the Affiliatewindow XML feed. I have been using version 1.0 for years but an issue has arisen with Dixons delivery costs in their 1.0 which I am told is impossible to fix therefore I must use version 1.2. But I cannot work out how to parse it.

New code for a specific product looks like this:

Quote:
- <prod awId="11568383" lang="EN" forSale="yes" preOrder="no" webOffer="no" inStock="yes">
<pId>669548</pId>
<name>SYMANTEC WBW PC/LA P NEW</name>
<desc>SYMANTEC EXCLUSIVE 06 + GHOST + GO.BACK</desc>
<spec>NORTON INTERNET SECURITY</spec>
- <cat>
<awCatId>68</awCatId>
<awCat>Software</awCat>
<mCat>Antivirus &amp; Security</mCat>
</cat>
<brand>SYMANTEC</brand>
<purl>Currys - Shop for Antivirus & Security - SYMANTEC 2006 <script>document.write(sym3)</script> Info</purl>
<mThumb>http://www.currys.co.uk/images/669548_01_small.jpg</mThumb>
<mImage>http://www.currys.co.uk/images/669548_01_large.jpg</mImage>
<awLink>http://www.awin1.com/pclick.php?p=11568383&a=143&m=1599</awLink>
<awThumb>http://images.productserve.com/thumb/1599/11568383.jpg</awThumb>
<awImage>http://images.productserve.com/preview/1599/11568383.jpg</awImage>
<delCost>4.95</delCost>
- <price curr="GBP">
<display>GBP59.99</display>
<search>59.99</search>
</price>
</prod>
The problem is that some tags are nested within other tags - <prod>, -<cat>, -<price> and I don't know how you parse info from these sub-tags.

I want to extract info from the following tags:

<name>
<awcatid>
<brand>
<awlink>
<delcost>
<price>

Can anyone help please?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 29-08-07
Registered User
 
Join Date: Mar 2004
Location: Reading, UK
Posts: 301
Thanks: 0
Thanked 0 Times in 0 Posts
dmorison is an unknown quantity at this point
  Re: Parsing Affiliatewindow XML Feed 1.2

Hi Andy,

Can you post the section of code that you are currently using to parse the <prod> element, it will be easier to advise what you need to add to access the nested elements. Alternatively, if you've not made much progress with this format post the code you were using with the old version...

Alternatively (sneaky plug), Magic Parser works great with the new aff win feeds (link in sig)....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 29-08-07
Registered User
 
Join Date: Aug 2003
Posts: 218
Thanks: 1
Thanked 0 Times in 0 Posts
Andy is an unknown quantity at this point
  Re: Parsing Affiliatewindow XML Feed 1.2

Quote:
Originally Posted by dmorison View Post
Hi Andy,

Can you post the section of code that you are currently using to parse the <prod> element, it will be easier to advise what you need to add to access the nested elements. Alternatively, if you've not made much progress with this format post the code you were using with the old version...

Alternatively (sneaky plug), Magic Parser works great with the new aff win feeds (link in sig)....
Thanks D Morison. I will post the code I used with the old version but I have changed the XML tag names in function characterdata_dixons to appropriate ones.

PHP Code:
function refresh_dixons($mycategory) {
 global 
$mycategory;

$xml_parser xml_parser_create();
xml_set_element_handler($xml_parser"startElement_dixons""endElement_dixons");
xml_set_character_data_handler($xml_parser"characterData_dixons");
$xmlurl="http://datafeeds.productserve.com/datafeed_products.php?user=xxx&password=mysecretpassword&mid=1597&format=XML&dtd=1.2";
 

$fp fopen($xmlurl,"r")
    or die(
"Error reading Xml data.");
while (
$data fread($fp4096))
    
xml_parse($xml_parser$datafeof($fp))
        or die(
sprintf("XML error: %s at line %d"
            
xml_error_string(xml_get_error_code($xml_parser)), 
            
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);  } 
Which uses the following functions

PHP Code:

function startElement_dixons($parser$name1$attrs) {
    global 
$insideitem$tag$name$delivery$awlink,  $brand$price$awcatid;

if (
$insideitem) {
        
$tag $name1;
    } elseif (
$name1 == "PRODUCT") {
        
$insideitem true;
    }
}

function 
endElement_dixons($parser$name1) {
    global 
$insideitem$tag$name$delivery$awlink,  $brand$price$awcatid;
if (
$name1 == "PRODUCT") {
     if (
$mycategory=="Washing Machines" and $mastercategoryid==535) { 
        
echo 
$name" "$brand" ",$awlink" ",  $price," "$delivery"<br>";
      }    

                                
$price "";
        
$name "";
        
$brand "";
        
$delivery "";
        
$awlink "";
        
$awcatid="";
        
$insideitem false;
    }
}

function 
characterData_dixons($parser$data) {
    global 
$insideitem$tag$name$delivery$awlink,  $brand$price$awcatid;
        if (
$insideitem) {
    switch (
$tag) {
        case 
"NAME":
        
$name .= $data;
        break;
        case 
"BRAND":
        
$brand .= $data;
        break;
        case 
"PRICE":
        
$price .= $data;
        break;
        case 
"AWLINK":
        
$awlink .= $data;
        break;
        case 
"DELCOST":
        
$delivery .= $data;
        break;
        case 
"AWCATID":
        
$awcatid .= $data;
        break;
                
    }
    }

It is the functions startelement_dixons and endelement_dixons which are the problem I think as they have to cope with several tags in the new version: <prod>, <cat> & <price>.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 29-08-07
Registered User
 
Join Date: Mar 2004
Location: Reading, UK
Posts: 301
Thanks: 0
Thanked 0 Times in 0 Posts
dmorison is an unknown quantity at this point
  Re: Parsing Affiliatewindow XML Feed 1.2

Hi Andy,

The trick to handling nested XML as opposed to flat XML is to keep track of the PATH. In other words, where you are currently setting the variable $tag within the startElement function to just the name of the current tag, what you need to be doing now is APPENDING the current tag to the current path, that is the collection of tag names that make up where you are now in the XML tree.

Then, within your endElement handler, you subtract the tag name from the current path.

The easiest way to do this is to maintain 2 global variables. The first, just as you are doing now, is the current "tag", call this $tag. The second variable that you need to develop is $path, which is all the $tag's concatenated together. To do this, where you currently have the following code:

PHP Code:
    if ($insideitem)
    {
        
$tag $name1;
    } 
...you would be looking at this:


PHP Code:
    if ($insideitem)
    {
        
$tag $name1;
        
$path $path $tag;
    } 
Now, here's the trick - within the endElement handler, you remove the current $tag from the current $path by using strlen($tag) and taking that many characters off the end. To do this, at the END of your endElement_dixons() function, you would do something like this:

PHP Code:
    if ($insideitem)
    {
        
$len strlen($tag); // remember that $tag holds the deepest tag you have come into
        // now strip that back off the $path
        
$path $path substr($path,0,(strlen($path)-strlen($tag)));
    } 
Having done that, you will now find that within your characterData handler that instead of using $tag, you can now use $path to decide what variable you are looking at. For example, the Affiliate Window category ID would now be indicated by a $path variable of "catawCatId".

Remember throughout all of the above to make $path a global variable, just as $tag.

Hope this points you in the right direction!
D.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 29-08-07
tbp tbp is offline
Registered User
 
Join Date: Dec 2006
Posts: 1,999
Thanks: 0
Thanked 18 Times in 18 Posts
tbp is an unknown quantity at this point
  Re: Parsing Affiliatewindow XML Feed 1.2

If you are using PHP 5, there is a MUCH simpler way

See the following page of the PHP Manual:

PHP: simplexml_load_file - Manual

As an example, the following code is all thats needed to pull out the details you require from the feed you gave:

PHP Code:
<?php
$xml 
simplexml_load_file("/home/user_dir/public_html/test/awProductFeed.xml");

foreach(
$xml->merch->prod as $product){
    echo 
"Name: " $product->name "<br>";
    echo 
"AW Cat ID: " $product->cat->awCatId "<br>";
    echo 
"Brand: " $product->brand "<br>";
    echo 
"AW Link: " $product->awLink "<br>";
    echo 
"Delivery Cost: " $product->delCost "<br>";
    echo 
"Price: " $product->price->search "<br>";
    echo 
"--------------------------" "<br>";
}
?>
Thats literally all thats needed! You just need to change the path to point to the feed file.

Instead of echoing the data to the screen, you can do whatever you want with it, such as insert into a database.

For the record you gave it would output:

PHP Code:
NameSYMANTEC WBW PC/LA P NEW
AW Cat ID68
Brand
SYMANTEC
AW Link
http://www.awin1.com/pclick.php?p=11568383&a=143&m=1599
Delivery Cost4.95
Price
59.99 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 29-08-07
Registered User
 
Join Date: Aug 2003
Posts: 218
Thanks: 1
Thanked 0 Times in 0 Posts
Andy is an unknown quantity at this point
  Re: Parsing Affiliatewindow XML Feed 1.2

Thanks guys. I will investigate this tomorrow.

Simplexml_load_file sounds the ticket.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 30-08-07
Registered User
 
Join Date: Aug 2003
Posts: 218
Thanks: 1
Thanked 0 Times in 0 Posts
Andy is an unknown quantity at this point
  Thumbs up Re: Parsing Affiliatewindow XML Feed 1.2

Quote:
Originally Posted by tbp View Post
If you are using PHP 5, there is a MUCH simpler way

See the following page of the PHP Manual:

PHP: simplexml_load_file - Manual
Thanks tbp.

I had to get my webhosting company to upgrade my php version but simplexml_load_file() works a treat and makes working with XML feeds childsplay.

Nice one
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 30-08-07
tbp tbp is offline
Registered User
 
Join Date: Dec 2006
Posts: 1,999
Thanks: 0
Thanked 18 Times in 18 Posts
tbp is an unknown quantity at this point
  Re: Parsing Affiliatewindow XML Feed 1.2

No problem!

It does make working with XML much easier than the previous methods of working through the file line by line and having to keep track of the tag structure etc.

However, the only thing that you need to watch is memory usage. It has to load and store all the xml data into memory to allow you to work with it in this way.

Its fine for most feeds, but if you have a merchant with tens of thousands of products if you are on a shared server it might be a bit much for it. In which case you would have to go back to processing the file line by line, as that way it only has to store small chunks in the servers memory at any one time.

Its a case of just keeping an eye on memory usage, there are lots of variables such as length of the file, number of fields in a record, whether your on a dedicated or shared server, amount of memory on the server etc.

As I said though for most feeds it will work fine and memory won't be an issue. For really large feeds its best to try the simple method first, if it puts too much strain on the server go back to the line by line processing.
Digg this Post!Add Post to del.icio.us