| By Chris Schalk | Article Rating: |
|
| January 10, 2008 11:00 AM EST | Reads: |
6,131 |
With Google's AJAX Feed API, however, pulling down Internet feed data and integrating it into your Web pages has never been so easy! It's like Google's Maps API, where all you need is a bit of JavaScript code on your page and you instantly have a usable Map. With a bit of JavaScript on your page, you can easily integrate Internet feed data into your Web pages.
In Figure 1, the Blogroll and Photos content on the right is derived from Internet feeds. This is easily doable in just a few lines of JavaScript. Before jumping into examples of Google's AJAX Feed at work, let's review a bit the Feed API's architecture.
The AJAX Feed API's Architecture at a Glance
Google's AJAX Feed API is a combination of a hosted service provided by Google along with a lightweight JavaScript client API that allows for the easy integration of public RSS and Atom feeds into any Web page via JavaScript. At its core, the AJAX Feed API relies on a Google service known as FeedFetcher that continuously scours the Internet for feeds and caches them on Google's worldwide servers, similar to how Google caches all other Internet data. Since feed data is constantly updated, the FeedFetcher is constantly refreshing its caches with fresh data.
As the feed data is cached on Google's servers, the AJAX Feed JavaScript API provides an easy way for a client Web page to make requests and pull down any feed data. The API does this by injecting a script tag onto the client page, which then makes the feed data request directly from Google without violating the same origin security policy. The API has provisions for the user to specify a callback function so when the request is complete it will call the provided callback function that can render the feed data. The user-provided callback function can either be low-level JavaScript code that iterates through the raw feed data and updates the user interface via DOM methods, or it can be just a few lines calling one of the available higher-level controls such as the FeedControl or the feed SlideShow. The overall AJAX Feed architecture is displayed in Figure 2.
The AJAX Feed API provides flexible ways to display feed data. At a low level, it provides both JSON and/or XML output to the Web page. The JSON output is a unified, or canonicalized, output of data from the original RSS or Atom feeds. Why is it canonicalized? This is done to provide a single way of extracting feed data as opposed to having to parse the different feeds separately because of their different RSS and Atom specifications. With the unified feed elements, it's easy to work with multiple feeds of multiple formats using the same code. (Figure 3)
Using the AJAX Feed API
To get a feel for how to work with the AJAX Feed, let's review some basic examples for starters. As mentioned earlier, the AJAX Feed API is a JavaScript API that communicates with Google's worldwide servers to provide feed data to your client Web page. As with the other JavaScript APIs that Google provides, one first has to get the API key that's issued after agreeing to a Terms of Services document. Once you have your API key, you can begin coding in JavaScript and build your first example such as a HelloWorld. In fact, Google provides some initial examples to help you get started.
An AJAX Feed HelloWorld Example
There's a simple HelloWorld example in Listing 1 that illustrates how to download an Internet feed from digg.com and display some of its data on a Web page.
In that example, we see how to load the AJAX Feed API using the new common loader syntax by specifying both the name of the API ("feeds") and its version ("1"). Incidentally the other AJAX APIs, AJAX Search and Google Maps also support this syntax.
After loading the Feed API, a function called initialize( ) is defined. This function creates a new google.feeds.Feed instance and specifies that it should load the feed located at www.digg.com/rss/index.html. The remainder of the function establishes an in-line callback function to process the returned data. Since no data type is specified, the default data is in JavaScript's native JSON format and is loaded into the result object. The remainder simply iterates through the data in the result object and uses the DOM methods, createElement( ), createTextNode( ), and appendChild( ) to write the returned feed data directly to the parent document by putting it inside of the empty <div> element with id="feed."
Figure 4 shows how the HelloWorld example will render in a browser.
Extending the HelloWorld Example - Displaying Multiple Feeds
As you get more familiar with the AJAX Feed API, you'll find it quite easy to display the returned data in more interesting ways. For example, here's an example that illustrates how multiple feeds in different formats will display in the same manner. This page is merely an extended version of the HelloWorld example above that lets the user select or enter his own Feed URL. In stepping through this demo, you see how the AJAX Feed API provides unified access to any supported RSS or Atom feed without having to parse them independently.
As you can see in Figure 5, it displays the BBC's RSS News Feed. Figure 6 shows the same Web page but pointing to a different feed URL. In this case it's Yahoo's Weather Feed.
Creating a page that displays feed data this way is relatively straightforward. We have a portion of the relevant code from this example. Aside from a standard HTML form on the page, there's an empty portion beneath the form that's populated dynamically by calling the JavaScript function showFeed( ). This function accepts a feed URL passed from the HTML form.
The code that populates the lower part of the page is in Listing 2.
You can see in the code that, as the showFeed( ) function is executed, it first creates a new Feed object with the chosen feedURL passed to it from the HTML form. The rest of the code works to display the feed data using DOM methods. First it displays core feed information such as the title, link, description, and author. These are attached to an existing <div> with id = "banner-div." It then generates an HTML table dynamically to display the repeating feed data from the entries array and attaches it to an existing <div> with id="feed-div."
Displaying Feed Data More Compellingly - GeoCoded Photos on a Google Map
Dumping feed data into an HTML table is somewhat interesting, however there are more interesting and compelling ways to display feed data. For example, it's possible to use the AJAX Feed API to extract geo-encoded data and then display it on a Google Map.
Here's a fun mash-up where the AJAX Feed API extracts geo-encoded photos from a feed and then uses the Google Maps API to display the data.
As shown in Figure 7, this example displays photos that correspond to one or a set of tags. In this case the single tag, "Paris," is used as a query parameter to the Flickr photo feed: http://api.flickr.com/services/feeds/photos_public.gne?tags=geo,paris&ge...
Also notice that an additional parameter, "georss=true," is also provided. This triggers the feed to supply only photos that are geo-encoded in the feed.
The raw data with the pertinent parts highlighted as it is returned from the feed is in Listing 3. You'll see that this feed contains a series of entries that have both HTML snippets in <content> tags and latitude and longitude information encoded in a <geo:Point> tag.
Published January 10, 2008 Reads 6,131
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Chris Schalk
Chris Schalk is a Tech Lead in the Developer API Evangelism group at Google. He is also one of the original members of the OpenAjax Alliance. Prior to Google, Chris was a Principal Product Manager and Java Evangelist for Oracle's application server and development tools division. While at Oracle, he worked to define the overall web development experience for Oracle JDeveloper and ADF Faces (Trinidad). He is also the co-author of JavaServer Faces: The Complete Reference published through McGraw-Hill Osborne (ISBN: 0072262400) and maintains a popular Blog on J2EE Web development at www.jroller.com/page/cschalk.
![]() |
Vidal 04/15/08 09:01:30 AM EDT | |||
Hi, I've just finished my new vertical job search engine JobGeni http://www.jobgeni.com that runs on Google AJAX Feed API. It's pulls the data from several major jobsites like indeed, simplyhired, yahoo hotjobs, monster and jobster. |
||||
- Kindle 2 vs Nook
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Windows 7 – Microsoft’s First Step to the Cloud
- Ulitzer Provides a Powerful Social Journalism Platform
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Open Source Mobile Cloud Sync and Push Email
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- US Post Office Hops a Ride on NetSuite’s Cloud
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Building a Drag-and-Drop Shopping Cart with AJAX
- What Is AJAX?
- Google Maps! AJAX-Style Web Development Using ASP.NET
- Flashback to January 2006: Exclusive SYS-CON.TV Interviews on "OpenAjax Alliance" Announcement
- AJAXWorld Conference & Expo to Take Place October 2-4, 2006, at the Santa Clara Convention Center, California
- AJAX Sponsor Webcasts Are Now Available at AJAXWorld Website
- How and Why AJAX, Not Java, Became the Favored Technology for Rich Internet Applications
- "Real-World AJAX" One-Day Seminar Arrives in Silicon Valley
- AJAXWorld University Announces AJAX Developer Bootcamp
- AJAX Support In JadeLiquid WebRenderer v3.1
- Where Are RIA Technologies Headed in 2008?
- Struts Validations Framework Using AJAX




































