RSS Headlines Aggregator - Validate XML?
Posted: Tue Jun 03, 2008 6:36 pm
Hi, I put a small section on my website to display news headlines. For each headline, I load the rss xml feed, put each item into an array, and then display the most recent headline. It's been working pretty well. The problem is if there are any mistakes in the code in the xml feeds, a whole bunch of errors are displayed on my page instead.
Questions:
1. I've been looking around on the internet as much as I can, and it seems like maybe I need to validate the xml first. Is this correct?
2. Or, is there a way to rewrite the code so it allows more flexibility for less than perfect xml?
3. Or, if there were a way to say don't display the errors if there are issues, that would work as well.
I'm sure there won't be errors often as the news sources are the NY Times, CNN Money, and other big name news companies, but it'd be nice to not have to worry about it. I'm fairly new to php, but I'm willing to learn. If you know of a good function for this purpose, I'd appreciate the help. Here's the code I'm using:
In the first part, I'm loading the xml file from the first source, and putting the items into arrays:
Secondly, some of the web addresses use ampersands, so I replace them with "&" so my xhtml will validate:
In the third part, I display the headlines as unordered xhtml lists:
That's just for one headline to simplify this, but I do that basically for five separate news sources and display each headline at the end. This has me a bit stumped. Thanks in advance for your help.
Rob
Questions:
1. I've been looking around on the internet as much as I can, and it seems like maybe I need to validate the xml first. Is this correct?
2. Or, is there a way to rewrite the code so it allows more flexibility for less than perfect xml?
3. Or, if there were a way to say don't display the errors if there are issues, that would work as well.
I'm sure there won't be errors often as the news sources are the NY Times, CNN Money, and other big name news companies, but it'd be nice to not have to worry about it. I'm fairly new to php, but I'm willing to learn. If you know of a good function for this purpose, I'd appreciate the help. Here's the code I'm using:
In the first part, I'm loading the xml file from the first source, and putting the items into arrays:
Code: Select all
$doc1 = new DOMDocument();
$doc1->load('http://www.nytimes.com/services/xml/rss/nyt/Business.xml');
$arrFeeds1 = array();
foreach ($doc1->getElementsByTagName('item') as $node1) {
$itemRSS1 = array (
'title' => $node1->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node1->getElementsByTagName('link')->item(0)->nodeValue,
);
array_push($arrFeeds1, $itemRSS1);
}
Code: Select all
$ny = str_replace("&","&","<li><a href=\"".$arrFeeds1[0]['link']."\" class=\"feed\">".$arrFeeds1[0]['title']."</a><span class=\"source\"> - NY Times</span></li>\n");
Code: Select all
echo "<ul>\n";
echo $ny;
echo "</ul>\n";
Rob