Article
PHP and XML: Parsing RSS 1.0
Another OOPtion
The more experienced programmers in the audience may have cringed as soon as I mentioned the use of global variables. There is a school of thought that global variables are merely a sign of lazy programming, and indeed PHP's Object Oriented Programming (OOP) features provide a better option. Here's an alternate version of the script we have just developed. Instead of functions to handle the XML document events, we use the methods of a PHP object. The data that these methods must share can then be stored as instance variables of the object, thus eliminating the need for global variables in our script.
class RSSParser {
var $insideitem = false;
var $tag = "";
var $title = "";
var $description = "";
var $link = "";
function startElement($parser, $tagName, $attrs) {
if ($this->insideitem) {
$this->tag = $tagName;
} elseif ($tagName == "ITEM") {
$this->insideitem = true;
}
}
function endElement($parser, $tagName) {
if ($tagName == "ITEM") {
printf("<p><b><a href='%s'>%s</a></b></p>",
trim($this->link),htmlspecialchars(trim($this->title)));
printf("<p>%s</p>",
htmlspecialchars(trim($this->description)));
$this->title = "";
$this->description = "";
$this->link = "";
$this->insideitem = false;
}
}
function characterData($parser, $data) {
if ($this->insideitem) {
switch ($this->tag) {
case "TITLE":
$this->title .= $data;
break;
case "DESCRIPTION":
$this->description .= $data;
break;
case "LINK":
$this->link .= $data;
break;
}
}
}
}
$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://www.sitepoint.com/rss.php","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($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);
If you're familiar with object oriented programming in PHP, then the only line that may be a source of consternation for you is the following:
xml_set_object($xml_parser,&$rss_parser);
Since xml_set_element_handler and xml_set_character_data_handler cannot take references to the methods of objects as the function names of the event handlers (i.e. xml_set_element_handler($xml_parser,"$rss_parser->startElement",... won't work), you need a way to tell the XML parser to call methods of the $rss_parser object instead of basic functions. xml_set_object does just that, taking the parser as well as a reference (note the &) to the object whose methods you want it to call.
Download the full code of this Object Oriented version here.