RSS Viewer Applet: Window to the World of News (3/4) - exploring XML
RSS Viewer Applet: Window to the World of News
RSSChannel class
This class holds all the information about one news channel, it mirrors the RSS file in structure and content. All the channel items are stored in a vector, with entries for item title and link alternating. This is not very nice but simple.
package com.exploringxml.rss; import com.microstar.xml.*; import java.util.Vector; import java.net.URL; public class RSSChannel { String channelTitle; URL channelLink; String channelDescription; String imageTitle; URL imageURL; URL imageLink; Vector items; public RSSChannel() { items = new Vector(); } public void load(String srcURL) throws Exception { XmlHandler handler = new RSSHandler(this); XmlParser parser = new XmlParser(); parser.setHandler(handler); parser.parse(srcURL, null, (String) null); }
The channel object instantiates the handler and the parser, and tells the parser to parse the RSS file from the source location of srcURL. The parser triggers the handler in the parsing process, and the handler fills in the channel attributes as a consequence. Parsing done! Not that difficult, right?
public String getChannelTitle() { return channelTitle; } public void setChannelTitle(String s) { channelTitle = s; } public URL getChannelLink() { return channelLink; } public void setChannelLink(URL u) { channelLink = u; } public String getChannelDescription() { return channelDescription; } public void setChannelDescription(String s) { channelDescription = s; } public String getImageTitle() { return imageTitle; } public void setImageTitle(String s) { imageTitle = s; } public URL getImageLink() { return imageLink; } public void setImageLink(URL u) { imageLink = u; } public URL getImageURL() { return imageURL; } public void setImageURL(URL u) { imageURL = u; } public String getItemTitle(int pos) { return (String) items.elementAt(2*pos); } public void setItemTitle(String s, int pos) { items.insertElementAt(s, 2*pos); } public URL getItemLink(int pos) { return (URL) items.elementAt(2*pos+1); } public void setItemLink(URL u, int pos) { items.insertElementAt(u, 2*pos+1); } public int getNumberOfItems() { return items.size() / 2; } public static final String ChannelTag = "channel"; public static final String ImageTag = "image"; public static final String ItemTag = "item"; public static final String TitleTag = "title"; public static final String LinkTag = "link"; public static final String DescriptionTag = "description"; public static final String URLTag = "url"; }
Finally some boring getter and setter methods for all of the attributes, plus some string constants for the expected RSS tags. Who calls the load() method? Let's delve into the class that ties it all together: RSSViewerApplet.
Produced by Michael Claßen
All Rights Reserved. Legal Notices.
URL: https://www.webreference.com/xml/column7/2.html
Created: Feb. 08, 2000
Revised: Feb. 08, 2000