Browse DevX
Sign up for e-mail newsletters from DevX


Java Package Processes All RSS Formats : Page 2

Several Java packages work with RSS. Some can read certain formats, while others can write them as well. The class this article features reads all known RSS formats and outputs the converted data in the 2.0 format.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:
  • A type name
  • A value
  • A collection of attributes
  • A collection of subelements (which are also instances of BasicXmlData)

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 like—there are 21 setters in total).

Twenty-three getter methods in com.myjavatools.xml.Rss—getCategory(), 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:

  • getCloud() returns an instance of XmlData that contains data as defined for <cloud> element.
  • getItem(String title) returns an item with the specified title.
  • getItems() returns a Collection that contains all the item elements of the RSS instance.
  • getTextInput returns the TextInput element.
  • 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
    Suppose you need to retrieve a RSS feed and display it in your Web page. I'll use my favorite flavor of Java, JSP, to show you how to do this with com.myjavatools.xml.Rss. Review the following JSP code:

    <html> <head> <title>Rss Feed Test</title> </head> <body> <% String url = request.getParameter("url"); if (url != null) { com.myjavatools.xml.Rss rss = new com.myjavatools.xml.Rss(new java.net.URL(url)); %> <h3> Rss feed from url <a href="<%=url%>"><%=url%></h3> title: <%=rss.getTitle()%><br /> category: <%=rss.getCategory()%><br /> description: <%=rss.getDescription()%><br/> language: <%=rss.getLanguage()%><br /> date: <%=rss.getLastBuildDate()%><br /> webmaster: <%=rss.getWebMaster()%><br /> <table border="1"> <tr><th >title</th><th>url</th><th>description</th><th>date</th><th>author</th> </tr> <% for (java.util.Iterator i = rss.getItems().iterator(); i.hasNext();) { com.myjavatools.xml.Rss.Item item = (com.myjavatools.xml.Rss.Item)i.next(); %> <tr> <td><%=item.getTitle()%> </td> <td><%=item.getEnclosureUrl()%> </td> <td><%=item.getDescription()%> </td> <td><%=item.getPubDate()%> </td> <td><%=item.getAuthor()%> </td> </tr> <% }%> </table> <%}%> <form action="rsstest.jsp"> Enter Rss Url:<input name="url" type="text" /> <input type="submit" /> </form> </body> </html>

    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
    The com.myjavatools.xml.Rss class also comes with a set of unit tests, with samples of all versions of RSS. Take a look at a sample code snippet from the included RSS 0.91 Unit Test:

    data_091 = new Rss(new URL("http://www.xml.com/cs/xml/query/q/19")); System.out.println("Title:\t" + data_091.getTitle()); System.out.println("Webmaster:\t" + data_091.getWebMaster()); System.out.println("Description:\t" + data_091.getDescription()); for (Iterator i = data_091.getItems().iterator(); i.hasNext();) { Rss.Item item = (Rss.Item)i.next(); System.out.println(" Item:\t" + item.getTitle()); System.out.println(" Description:\t" + item.getDescription()); System.out.println(" Link:\t" + item.getLink()); System.out.println(); }

    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.
    Comment and Contribute






    (Maximum characters: 1200). You have 1200 characters left.



    Thanks for your registration, follow us on our social networks to keep up-to-date