|
|||||||||
|
RSS in Java
As a decorator class for com.myjavatools.xml.BasicXmlData (see Practical XML for Java Programs), com.myjavatools.xml.Rss inherits all the standard features of XmlData:
The com.myjavatools.xml.Rss class defines three static member classes corresponding to popular RSS subelements: Rss.Image, Rss.TextInput, and RssItem. Other subelements, such as cloud, are just plain instances of BasicXmlData. The class' constructors instantiate RSS from a File, Url, InputStream, or XmlData. You can also use the default constructor to create an empty RSS container and then build the container by adding items (addItem(Rss.Item item)) and setting various data (using setters such as setImage, setCopyright, setDescription, setTextInput, setWebmaster, and the likethere are 21 setters in total). Twenty-three getter methods in com.myjavatools.xml.RssgetCategory(), getCloud(), etc., up to getWebMaster()return values as various RSS elements. Most of these methods return a String, since many elements actually are just String values (e.g., getCategory(), getRating(), getSkipHours(), getWebMaster(), etc.) Other getters return complex values:
Item is the most important element for handling RSS data. So com.myjavatools.xml.Rss needs more functions for that element than just returning a collection of all items and retrieving an item by its title. For further functionality, it also includes more item finders: findByDescription(String description), findByUrl(String url), findByGuid(String guid). To accelerate item search by title, it indexes items in the RSS container; getItem(String title) uses a HashMap that works as an index table. Within a RSS object, it also strips descriptions in all elements (channel, item, image, textInput) of their leading and trailing space characters. These characters have no meaning in human-readable descriptions and their absence makes search by description more reliable. In addition, you can use the methods inherited from BasicXmlData to save a RSS object to a File or to send it to an OutputStream. As previously stated, the output RSS format is always 2.0.
Sample JSP Using RSS
If you run this JSP, and enter a popular RSS URL in the form (say, http://slashdot.org/index.rss), you will get the latest Slashdot news formatted in a table (see Listing 1).
Testing and Packaging
See Listing 2 for the full listing, or download the whole package, com.myjavatools.xml, with the library, mjxml.jar. Click here to view Java documentation for the package.
|
|||||||||
|
Vlad Patryshev is an R&D engineer at the Java Business Unit of Borland. He recently started the myjavatools.com project.
| |||||||||
|