Creating a parser

The component does support the major formats of CSV files. Using the mapping in feed configuration any CSV file (with a header row) should work. Problem might be that the 'description' field has a different name, and there is no mapping for the description. This can be solved easily using a callback function.

If the data is only available in xml format the existing parsers will  probably not work. Still you might give it a try many feeds use 'item' or 'product' as tag and the Affiliates (item) or Tradetracker (product) parser might work.

Otherwise the solution is to add you own parser. The component allows to add two custom parsers:

Create a file xml_custom.inc in administrator/components/com_datafeeds/cron

In this file you can put your own parser. Most basic duplicating the existing parser: (SVN:1035)

Class MyXMLParser extends TradetrackerRSS{
        
}

$feed_parsers['MyXMLParser']='My Own Parser';

Next step would be to copy the relevant functions from the TradetrackerRSS class (start element and end element) and alter the code.

On the page about 'adding share a sale' feeds you can find an example of a custom parser.

 

Example basic parser

The parser below can be used for simple xml structures without tree's or attributes. The class is included in recent versions (svn:1149) of xml_fetch.inc

<?php
###########################
# for 'any' simple xml structure
# <container>
#  <item>
#   <tag1>value</tag1>
#   <tag2>value</tag2>
#  </item>
#  <item>
#   <tag1>value</tag1>
#   <tag2>value</tag2>
#  </item>
# </container>
#
#
#########################
class BasicRSS extends TradetrackerRSS{
        var 
$item_tag='item';

         function 
feed_start_element($p$el, &$attrs) {
        
$el =  strtolower($el);
        if (
$el == $this->item_tag ) { $this->initem true; }
        else {
                if ( 
$this->initem ) {
                        
array_unshift($this->stack$el);
                }
        }
    }

    function 
feed_end_element ($p$el) {
        
$el strtolower($el);

        if ( 
$el == $this->item_tag ) {
            
$this->store_item($this->current_item);
            
$this->initem false;
            
$this->el_count=array();
        }
        else {
                if ( 
$this->initem ) {
                        
array_shift($this->stack);
                }
        }
    }
}


Usage

Either copy the code into the file xml_custom.inc and modify the code as desired. Or use a child class

 

<?php
class CarRSS extends BasicRSS{
        var 
$item_tag='car';
}

$feed_parsers['CarRSS']='Car Parser';

 

This parser will parse the xml below, however tweaks are needed since the mandatory field 'description' is not present, and the image's will pile up. This can be solved using a callback function, or by customizing the parser.

 
<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<car>
<regno>XXX111</regno>
<brand>Audi</brand>
<model>A4</model>
<modeldescription>1.8 T Avant, Proline</modeldescription>
<yearmodel>2004</yearmodel>
<firstreg></firstreg>
<miles>17400</miles>
<price-sek>79.000</price-sek>
<extrapris></extrapris>
<bodytype>Kombi</bodytype>
<fax></fax>
<color>Brilliant svart</color>
<phone>08-000 22 23</phone>
<homepage>http://www..se</homepage>
<email>info@.se</email>
<info>ABS uni 2010, </info>
<message>Välkommen </message>
<gearboxtype>Manuell</gearboxtype>
<fueltype>Bensin</fueltype>
<seller>JD Bil</seller>
<newcarstatus>0</newcarstatus>
<environment>0</environment>
<images>
<image>http://data..com/images/xoserv/UMZ621-1001.jpg</image>
<image>http://data..com/images/xoserv/</image>
</images>
</car>
</cars>


 

Plaats reactie


Beveiligingscode
Vernieuwen