XML

Creating and Validating a News Feed

You're finally ready to assemble a complete RSS news feed. So without further ado, take a look at Listing 24.1, which contains the complete source code for an RSS feed that is loosely based on my own personal blog.

Listing 24.1. The RSS Code for a Sample Blog
 1: <?xml version="1.0" ?>
 2:
 3: <rss version="2.0">
 4:   <channel>
 5:     <title>Blog</title>
 6:     <description>Technology, entertainment, culture, you name it...
        </description>
 7:     <link>http://www.xyz.com/</link>
 8:
 9:     <item>
10:       <title>My Car Has a Virus!</title>
11:       <description>This is a little disturbing but I suppose it was inevitable
12:       wireless networking technologies are now potentially opening up
13:       automobiles to computer viruses.</description>
14:       <link>http://www.xyz.com/mambo/index.php?option=
15:       com_content&amp;task=view&amp;id=229&amp;Itemid=37</link>
16:       <pubDate>Tue, 02 Aug 2005 10:20:44 CST</pubDate>
17:     </item>
18:
19:     <item>
20:       <title>Smart Personal Objects</title>
21:       <description>The technology is a couple of years old and it has yet to
22:       catch on in any real sense but I think it has some interesting potential.
23:       I'm referring to Microsoft's SPOT (Smart Personal Object Technology),
24:       which is currently deployed in several smart watches that are capable
25:       of receiving data over a wireless wide area radio network.</description>
26:       <link>http://www.xyz.com/mambo/index.php?option=
27:       com_content&amp;task=view&amp;id=227&amp;Itemid=37</link>
28:       <pubDate>Thu, 28 Jul 2005 00:17:07 CST</pubDate>
29:     </item>
30:
31:     <item>
32:       <title>RFID Pajamas</title>
33:       <description>Not sure how I feel about this one. A children's apparel
34:       maker in California is set to launch a line of pajamas with RFID chips
35:       sewn into them that can be used to track children.</description>
36:       <link>http://www.xyz.com/mambo/index.php?option=
37:       com_content&amp;task=view&amp;id=223&amp;Itemid=37</link>
38:       <pubDate>Mon, 18 Jul 2005 13:10:42 CST</pubDate>
39:     </item>
40:   </channel>
41: </rss>

This code reveals how a complete RSS document pulls together the different tags you've learned about to describe multiple news items within a single channel. The first step is to specify details about the channel, which are handled in lines 5 through 7. Once the channel is in place, you can then start listing out the news items. Each of the three news items in this example use the <title>, <description>, <link>, and <pubDate> tags to flesh out their details. Notice that the <pubDate> tag uses a consistent format throughout all of the items for specifying the date and time (lines 16, 28, and 38).

If you specify a "pubDate" that is in the future, some news aggregators may elect not to display the item until that date and time.

Hopefully you now have a pretty good feel for how an RSS document is structured. Even so, you don't have to trust my good word entirely. Instead, I encourage you to use a really handy online tool called the Feed Validator to make sure that your RSS feeds are coded properly. The Feed Validator is available online at http://www.feedvalidator.org/. Its job is to read an RSS feed and make sure that it validates against the RSS specification.

You can name your RSS documents with a .XML or .RSS file extensionboth are acceptable.

Once the mmblog.xml sample RSS document receives a clean bill of health from the Feed Validator, it's ready to be posted online where other people can syndicate it using their own news aggregator of choice. But your work isn't over. Not only is it important to know how to create RSS documentsit's just as useful knowing how to transform and display them.