Hi folks,
Firstly thanks opengeek for your suggestion on the last one....but im moving away from the newsparserX snippet!
The snippet below is used to parse an rss feed for use on a site. I have a problem with the newparserX snippet that I found on another post........and wanted to use a smaller simpler snippet
The problem remains in that when a snippet errors (sometimes unavoidable) the entire modx page errors with a modx parse error.
For example the line below $fp = fopen("
http://www.sitepoint.com/rss.php","r") or die("problems!"); may error due to the resulting rss feed being down, but if I die in a snippet, or get a warning the entire modx page will not display. Now without php 5 (try + catch) is there a way to handle a feed being down...and still show my modx page with maybe an message stating the rss feed is down??? Please help...snippet below.
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link;
if ($name == "ITEM") {
printf("<dt><b><a href=’%s’>%s</a></b></dt>",
trim($link),htmlspecialchars(trim($title)));
printf("<dd>%s</dd>",htmlspecialchars(trim($description)));
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
break;
}
}
}
$xml_parser = xml_parser_create();
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);