Hi,
I have build a small script to parse rss feeds.
<?php
class RSSParser {
var $_url;
var $_xmlParser;
var $_items;
/* Rss data vars */
var $_inside = false;
var $_tagname;
/* Data content */
var $_title;
var $_description;
var $_link;
function RSSParser($url)
{
$this->_url = $url;
$this->Parse();
}
function Parse()
{
/* Create the new xml parser */
$this->_xmlParser = xml_parser_create();
/* Set the event handlers */
xml_set_element_handler($this->_xmlParser, "StartElement", "EndElement");
xml_set_character_data_handler($this->_xmlParser, "charachterData");
/* Try to open the rss feed */
$fp = fopen($this->_url,"r") or die("Oops, something seems to be wrong with the news feed. Please contact the webmaster.");
/* Read the file contents */
while ($data = fread($fp, 4096))
xml_parse($this->_xmlParser, $data, feof($fp));
/* Close file and free parser */
fclose($fp);
xml_parser_free($this->_xmlParser);
}
function GetItems()
{
return $this->_items;
}
function StartElement($parser, $tagname, $attributes)
{
if($tagname == "ITEM")
{
$this->_inside = true;
}
if($this->_inside == true)
{
$this->_tagname = $tagname;
}
}
function EndElement($parser, $tagname)
{
if($tagname == "ITEM")
{
$this->_items .= array ( title => $this->_title, description => $this->_description, link => $this->_link );
$this->_title = "";
$this->_description = "";
$this->_link = "";
}
}
function charachterData($parser, $data)
{
if($this->_inside)
{
switch($this->_tagname)
{
case "TITLE":
$this->_title = $data;
break;
case "DESCRIPTION":
$this->_description = $data;
break;
case "LINK":
$this->_link = $data;
break;
}
}
}
}
?>
But the problem is that I get several parsing errors, because the event handlers could not be called. Does someone know what the problem is?
Warning: Unable to call handler StartElement() in c:\phpdev\www\experimenten\experimenten
RSSParser.php on line 33
Warning: Unable to call handler StartElement() in c:\phpdev\www\experimenten\experimenten
RSSParser.php on line 33
Warning: Unable to call handler charachterData() in c:\phpdev\www\experimenten\experimenten
RSSParser.php on line 33
Warning: Unable to call handler charachterData() in c:\phpdev\www\experimenten\experimenten
RSSParser.php on line 33
Warning: Unable to call handler StartElement() in c:\phpdev\www\experimenten\experimenten
RSSParser.php on line 33
Warning: Unable to call handler charachterData() in c:\phpdev\www\experimenten\experimenten
RSSParser.php on line 33
Warning: Unable to call handler EndElement() in c:\phpdev\www\experimenten\experimenten
RSSParser.php on line 33
Thanks in advanced!