|
Distributed Computing This website demonstrates using wikis as teaching and learning tool. The course instructor is also happy to share the teaching materials here with those who find it readable. |
Tutorial /
RSS Processing with JavaIntroductionThis is a tutorial for you to practice XML processing with Java. RSS stands for "Really Simple Syndication" and is used for the distribution of newsfeeds and podcasts. RSS feeds are merely XML files based on a version of the RSS standard. For example, the following URL is the XML file of RTHK Instant News RSS feed: http://www.rthk.org.hk/rthk/news/rss/c_expressnews.xml There are several frameworks that allow you to work with RSS. Sun has its own RSS library named RSS utilities, which provides some APIs for you to parse RSS easily. The following article provides you more information about the RSS utilities: I downloaded the RSS utilities form the following URL: and put the rssutils.jar into my Java classpath. I then write the following Java program to test the RSS parsing with Java. You can compile the program and test it yourself. import java.io.*; import java.net.*; import java.util.*; import com.sun.cnpi.rss.parser.*; import com.sun.cnpi.rss.elements.Rss; import com.sun.cnpi.rss.elements.Item; import com.sun.cnpi.rss.elements.Category; class TestRSS { public void readRSSDocument() throws Exception{ RssParser parser = RssParserFactory.createDefault(); Rss rss = parser.parse(new URL("http://www.rthk.org.hk/rthk/news/rss/c_expressnews.xml")); //Get all XML elements in the feed Collection items = rss.getChannel().getItems(); if(items != null && !items.isEmpty()) { //Iterate over our main elements. Should have one for each article for(Iterator i = items.iterator(); i.hasNext(); System.out.println()) { Item item = (Item)i.next(); System.out.println("Title: " + item.getTitle()); System.out.println("Link: " + item.getLink()); System.out.println("Description: " + item.getDescription()); } } //Iterate over categories if we are provided with any Collection categories = rss.getChannel().getCategories(); if(categories != null && !categories.isEmpty()) { Category cat; for(Iterator i = categories.iterator(); i.hasNext(); System.out.println("Category Domain: " + cat.getDomain())) { cat = (Category)i.next(); System.out.println("Category: " + cat); } } } public static void main(String A[]) { System.setProperty ("http.proxyHost","proxy.ouhk.edu.hk"); System.setProperty ("http.proxyPort","8080"); TestRSS rss = new TestRSS(); try { rss.readRSSDocument(); }catch(Exception e) {} } } Your TasksUse the Sun RSS API to write a program that reads a specified RSS feed, then parses it, formats and stores the results into a file for human reading. Modify the previous program so that it can do with more than one Rss feeds. ReferenceReading the News with Sun's RSS Utilities (by Chris Hardin on 03/21/2006) (URL: http://today.java.net/lpt/a/275) |