RSS and AJAX: A Simple News Reader
An Ajax RSS Parser
The RSS specification that we will be using is 2.0, which is both the newest and most widely used of the three specifications (0.98, 1.0, and 2.0). Fortunately, RSS 2.0 is far less complex than RSS 1.0, so you can quickly familiarize yourself with RSS 2.0 here: blogs.law.harvard.edu/tech/rss. If you want a comprehensive introduction to RSS, covering all three specifications, go here: www.xml.com/pub/a/2002/12/18/dive-into-xml.html.
Why are we using Ajax to parse our RSS? By using Ajax, we are passing over the work of processing the RSS XML file to the web browser, thus reducing server load. Also, Ajax allows the user to have a more seamless web experience, because we are able to fetch the entire RSS XML file from the server without having to refresh the page. Lastly, Ajax is designed to handle XML files, so it's able to parse RSS in a simple and elegant way.
Here's how the parser is going to work: first, the file name of the RSS feed is selected in an HTML form. Once the user clicks Submit, the
getRSS() function is called. This function is responsible for fetching the specified RSS XML file from the server. Once it's fetched successfully,
Figure 1. General design
The HTML File
To begin, we'll have a look at the HTML file. The top half (the
form element) determines which RSS feed to fetch, while the bottom half (the root
For now, we will ignore most of the HTML and focus on the form element (labeled
<!--A--> above). The names of the RSS XML files are specified in the
return false to prevent the entire form from being sent to the server the "conventional" way. If we'd omitted
<!--B-->). In case you're wondering, the contents of the
<style> tag (labeled
<!--C-->) tell the browser how to display the RSS data when it's written to the HTML page by the