Location: PHPKode > scripts > AmazonLiteXMLParser > amazonlitexmlparser/class.AmazonLiteXMLParser.inc
<?php
/*
    -
    AmazonLiteXMLParser ver 1.0.1
    Author: Daniel Kushner
    Email: hide@address.com
    Release: 20 July, 2002
    http://www.amazonlite.com/
    Last Update: 22 October, 2002
  
*/
define('AMAZON_FIELD_TYPE_SINGLE', 1);
define('AMAZON_FIELD_TYPE_ARRAY', 2);
define('AMAZON_FIELD_TYPE_CONTAINER', 3);
class AmazonLiteXMLParser {
    
    var $parser;
    var $record;
    var $currentField = '';
    var $fieldType;
    var $endsRecord;
    var $errorRecord;
    var $errorMsg;
    var $records;
    var $wroteElementData = false;
    
    function AmazonLiteXMLParser($xml) {
        
        $xml = preg_replace(array('/&amp;/',
                                  '/<p>/i', 
                                  '/<b>/i', 
                                  '/\'/', 
                                  '/\<br\>/i', 
                                  '/&/'), 
                            array('and', 
                                  '', 
                                  '', 
                                  '', 
                                  ''), trim($xml));
        $this->records = array();
        
        $this->parser = xml_parser_create();
        xml_set_object($this->parser, $this);
        xml_set_element_handler($this->parser, 'startElement', 'EndElement');
        xml_set_character_data_handler($this->parser, 'cdata');

        $this->fieldType = array('errormsg'         => AMAZON_FIELD_TYPE_SINGLE,
                                 'title'            => AMAZON_FIELD_TYPE_SINGLE,
                                 'authors'          => AMAZON_FIELD_TYPE_CONTAINER,
                                 'author'           => AMAZON_FIELD_TYPE_ARRAY,
                                 'asin'             => AMAZON_FIELD_TYPE_SINGLE,
                                 'isbn'             => AMAZON_FIELD_TYPE_SINGLE,
                                 'media'            => AMAZON_FIELD_TYPE_SINGLE,
                                 'productname'      => AMAZON_FIELD_TYPE_SINGLE,
                                 'catalog'          => AMAZON_FIELD_TYPE_SINGLE,
                                 'releasedate'      => AMAZON_FIELD_TYPE_SINGLE,
                                 'manufacturer'     => AMAZON_FIELD_TYPE_SINGLE,
                                 'imageurlsmall'    => AMAZON_FIELD_TYPE_SINGLE,
                                 'imageurlmedium'   => AMAZON_FIELD_TYPE_SINGLE,
                                 'imageurllarge'    => AMAZON_FIELD_TYPE_SINGLE,
                                 'listprice'        => AMAZON_FIELD_TYPE_SINGLE,
                                 'ourprice'         => AMAZON_FIELD_TYPE_SINGLE,
                                 'usedprice'        => AMAZON_FIELD_TYPE_SINGLE,
                                 'salesrank'        => AMAZON_FIELD_TYPE_SINGLE,
                                 'media'            => AMAZON_FIELD_TYPE_SINGLE,
                                 'nummedia'         => AMAZON_FIELD_TYPE_SINGLE,
                                 'availability'     => AMAZON_FIELD_TYPE_SINGLE,
                                 'avgcustomerrating' => AMAZON_FIELD_TYPE_SINGLE,
                                 'rating'           => AMAZON_FIELD_TYPE_ARRAY,
                                 'summary'          => AMAZON_FIELD_TYPE_ARRAY,
                                 'comment'          => AMAZON_FIELD_TYPE_ARRAY,
                                 'product'          => AMAZON_FIELD_TYPE_ARRAY,
                                 'directors'        => AMAZON_FIELD_TYPE_CONTAINER, 
                                 'director'         => AMAZON_FIELD_TYPE_ARRAY,
                                 'starring'         => AMAZON_FIELD_TYPE_CONTAINER, 
                                 'actor'            => AMAZON_FIELD_TYPE_ARRAY,
                                 'mpaarating'       => AMAZON_FIELD_TYPE_SINGLE
                                 );
                                 
        $this->endsRecord   = array('details' => true);
        $this->errorRecord  = array('errormsg' => true);
        
        xml_parse($this->parser, $xml);
        xml_parser_free($this->parser);
    }
    
    function startElement($p, $element, &$attributes) {
        $element =strtolower($element);

        if(isset($attributes['URL'])) {
            $this->record['url'] = $attributes['URL'];   
        }
            
        if(isset($this->fieldType[$element])) {
            $this->currentField = $element;
            
        } else {
            $this->currentField = '';
        }
        
        $this->wroteElementData = false;
    }
    
    function endElement($p, $element) {
        $element =strtolower($element);
        if(isset($this->endsRecord[$element])) {
            $this->records[] = $this->record;
            $this->record = array();
        }
        $this->currentField = '';
    }
    
    function cdata($p, $text) {
        $text = preg_replace('/lt;([a-z]+\>)/i', '<\\1', $text);
        
        if(isset($this->errorRecord[$this->currentField])) {
            $this->errorMsg = $text;
        }
        
        if(@$this->fieldType[$this->currentField] === AMAZON_FIELD_TYPE_CONTAINER) {
            
        } elseif(@$this->fieldType[$this->currentField] === AMAZON_FIELD_TYPE_ARRAY) {
            $lastIndex = @count($this->record[$this->currentField]) - 1;
            $this->wroteElementData ? 
                @$this->record[$this->currentField][$lastIndex] .= $text :
                @$this->record[$this->currentField][$lastIndex+1] = $text ;
        } elseif(@$this->fieldType[$this->currentField] === AMAZON_FIELD_TYPE_SINGLE) {
            @$this->record[$this->currentField] .= $text;
        }
        $this->wroteElementData = true;
    }
}
?>
Return current item: AmazonLiteXMLParser