- Structure of an XML Document
- Creating an RSS File
- Reader Exercises
- Listings
Creating an RSS File
Now that you have seen how to use RSS files and are familiar with their structure, it is a good time to explain how a developer can create his or her own channel file. To make an RSS file useful in an application, it is necessary to have the essential element a channel needsnews to share with othersthat is on the Web. For example's sake, we will assume that the information is kept in a text file. Of course, your information may be in a database or are HTML files in a special directory or some other type of data source.
The following example, make_rss, is not a CGI script. It is meant to be used as a command line script, which could be run from a schedule to automatically create the RSS file. This code, of course, could be run as a CGI, but unless your news changes by the minute, having it scheduled to run on intervals should suffice.
01: #!/usr/bin/perl -wT 02: # make_rss 03: use strict; 04: use XML::RSS;
Lines 1-4 define the path to Perl as well as pull in the strict pragma and XML::RSS module.
05: my $FILE = 'news.txt'; 06: my $RDF_DIR = './rdf';
Lines 5 and 6 create two scalar variables we will be using later. Line 5 initializes $FILE with the location of the text file that has the news information in it. This file, which we called news.txt, is a pipe delimited file with the URL for the news story to the left of the pipe and the description of the story on the right. On line 6 the $RDF_DIR variable is created. This variable holds the location of the directory in which the RSS file is to be created.
07: my $rdf = new XML::RSS;
Line 7 creates a new XML::RSS object. The reference to that object is being stored in $rdf.
08: $rdf->channel(title => 'My News', 09: link => 'http://news.me.com', 10: language => 'en', 11: description => 'My news, for you!', 12: copyright => 'Copyright 2000++, Me', 13: pubDate => scalar localtime(time), 14: lastBuildDate => scalar localtime(time), 15: managingEditor => 'me@me.com', 16: webMaster => 'me@me.com' 17: );
Lines 8-17 use the channel() method to define some of the main information about the channel itself and begin the "channel" container. Earlier in this chapter you were shown a complete RSS file, and you can see how each of these pairs is represented in the final document.
18: $rdf->image(title => 'My News', 19: url => 'http://news.me.com/my_news.gif', 20: link => 'http://news.me.com', 21: height => 30, 22: width => 119 23: );
Lines 18-23 create the "image" container. This is optional, but if there is an icon available for the channel, this is how to add it.
24: open(FILE, $FILE) || die "Can't open $FILE ($!)"; 25: while (<FILE>) { 26: my ($url, $desc) = split /\|/; 27: $rdf->add_item(title => $desc, 28: link => $url 29: ); 30: }
Lines 24-30 get the data from the text file and use it to create the "item" containers. Line 24 opens the file for reading or dies with an error. Lines 25-30 are a while() loop that iterates over each line of the file. Line 26 splits the input by the pipe (|) character and stores the values in the $url and $desc variables. Lines 27 and 28 use the add_item() method to add an item into the "channel" container. As you saw in the RSS file example, title and link are the two elements in the "item" container. By simply creating named value pairs as arguments to add_item(), we can create a new container. When this loop is complete all of the items will be in the "channel" container.
31: $rdf->textinput(title => 'Search My News', 32: description => 'Search the Archives', 33: name => 'text', 34: link => 'http://news.me.com/search.cgi' 35: );
Lines 31-35 use the textinput() method to create a "textinput" container. This is an optional container that will create a text box for people to use to search your site. The URL of the search script is the value of the "link" element, in which the name, title, and description of the link are the values of their corresponding key. At this point, the channel is created in the XML::RSS object.
36: $rdf->save("$RDF_DIR/my_news.rdf");
Line 36 ends the script by writing the file to disk using the save() methodand that easily a RSS file is created! This is the type of script that can be written and implemented in short time and needs little to no maintenance.
Now you are ready to take RSS full cycle from creating the data source, creating the RSS format file, and using that file for the Web.