RSS Parser





0
Date Submitted Fri. May. 1st, 2009 4:00 AM
Revision 1 of 1
Beginner subhasishweb
Tags "RSS Parser"
Comments 0 comments
The class containing how to parse rss and display records.

<?php
        /******************** Example ********************
        $rss_parser = new RSSParser();
        $rss_parser->xml_parser = xml_parser_create();
        $rss_parser->parse_results($rss_parser, "http://auctions.overstock.com/rss/seller/8162545/listings.xml");
        *************************************************/
     
        class RSSParser {
                private $title = "";
                private $link = "";
                private $description = "";
                private $inside_item = false;
                private $current_tag;
                public $xml_parser;
               
                private function startElement($parser, $name, $attrs='') {
                        $this->current_tag = $name;
                        if($this->current_tag == "ITEM") $this->inside_item = true;
                }
               
                private function endElement($parser, $tagName, $attrs='') {
                        if($tagName == "ITEM") {
                                printf("\t<br><b><a href='%s' target='_blank'>%s</a></b>\n", trim($this->link), htmlspecialchars(trim($this->title)));
                                print("<br>");
                                printf("\t<br>%s<br>\n", str_replace("<a","<a target='_blank'",trim($this->description)));
       
                                $this->title = "";
                                $this->description = "";
                                $this->link = "";
                                $this->inside_item = false;
                        }
                }
               
                private function characterData($parser, $data) {
                        if($this->inside_item) {
                                switch($this->current_tag) {
                                        case "TITLE":
                                                $this->title.= $data;
                                                break;
                                        case "DESCRIPTION":
                                                $this->description.= $data;
                                                break;
                                        case "LINK":
                                                $this->link.= $data;
                                                break
                                        default:
                                                break;
                                }
                        }
                }
               
                public function parse_results($rss_parser, $file) {
                        xml_set_object($this->xml_parser, &$rss_parser);
                        xml_set_element_handler($this->xml_parser, "startElement", "endElement");
                        xml_set_character_data_handler($this->xml_parser, "characterData");
                       
                        $fp = fopen("$file","r") or die("Error reading XML file, ".$file);
                       
                        while($data = fread($fp, 4096)) {
                                xml_parse($this->xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)), xml_get_current_line_number($this->xml_parser)));
                        }
                       
                        fclose($fp);
                        xml_parser_free($this->xml_parser);
                }
        }
?>
 

Subhasish Nag

Thanks,
--
Subhasish Nag
==============
Mobile: +91 9831581556

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Votes Down