Retrieving RSS/Atom Feeds with the Google AJAX Feed API

Using feeds to manage online news.

Receiving an Atom Feed

In this section, we’ll generate a JavaScript application to download and display an Atom feed for Reg Developer. First, obtain the URL for the Atom feed from the Reg Developer Feeds page.The URL for the Atom Feed “Reg Developer” is http://www.regdeveloper.co.uk/headlines.atom. To access a Feed with the Google AJAX Feed API we need to add the following <script> element to the JavaScript application GoogleRSS.html:

 <script type="text/javascript"
          src="http://www.google.com/jsapi?key=ABQIAAAAItAEL-H-
GuOHsDfD7MxmlhSpY8xpRlVNOvpHrXTaq4lZbJurjRQMBL0DtsRGauRDjwp6d-
ST2IgadQ"></script> 

The previous <script> tag loads the google.load function that is needed on order to load individual Google APIs. First, download version 1 of the feeds API:

 google.load("feeds", "1");

Then create a JavaScript function initialize and specify the function to be invoked when the Google Feed API has loaded:

 google.setOnLoadCallback(initialize); 

Next, create a Feed class object to download a feed. Specify the URL for the Reg Developer Feed, to create the Feed class object:

 var feed = new google.feeds.Feed("http://www.regdeveloper.co.uk/headlines.atom");

Now, load the Atom feed using the load(callbackFunction) method of the Feed class:

 feed.load(function(result) {        });

Next, add a <div> element to the <body> element of the JavaScript application to display the Atom feed results:

 <body><div id="feed"></div></body> 

Then, in the callback function obtain the <div> element using the getElementById method:

 var container = document.getElementById("feed");

Now, iterate over the feed results:

 for (var i = 0; i < result.feed.entries.length; i++) {       }

Then, obtain each of the feed entries, create a <div> element for each of the feed entries and add the entry title to the <div> element for each of the feed entry:

 var entry = result.feed.entries[i];           
 var div = document.createElement("div");            div.appendChild(document.createTextNode(entry.title)); 

And finally, add the <div> element for each of the entries to the container <div> element:

 container.appendChild(div); 

The JavaScript application GoogleRSS.html is available in the Resources zip file.

To display the Atom Feed entries’ titles, right-click on the GoogleRSS.html HTML page and select Run - see Figure 3:

Shows the JavaScript Applicationgeneration running.

The entries’ titles in the Atom feed are displayed – see Figure 4:

Shows the resulting Atom feed.

The previous Atom Feed application only displays the titles of the entries in the Atom feed. Next, we shall display additional JSON properties such as “title”, “link”, “publishedDate” and “contentSnippet” from the Atom Feed.

Sponsored: Network DDoS protection