FileDocCategorySizeDatePackage
RSSInfo.javaAPI DocExample3337Sat Nov 22 19:55:24 GMT 2003vhexample.view

RSSInfo

public class RSSInfo extends Object
A class for parsing RSS information from a file

Fields Summary
private static final String
CHANNEL_TAG
RSS tags for channel, item, title and link
private static final String
ITEM_TAG
private static final String
TITLE_TAG
private static final String
LINK_TAG
private Item[]
items
all the items in the RSS file
private Item
channel
the channel information (one per file)
Constructors Summary
Methods Summary
public java.lang.StringgetChannelLink()
Get the link associated with the channel

        return channel.link;
    
public java.lang.StringgetChannelTitle()
Get the title of the channel

    
               
       
        return channel.title;
    
private java.lang.StringgetElementValue(org.w3c.dom.Element parent, java.lang.String child)
Read a value from a text node in the XML

        // find the child
        NodeList list = parent.getElementsByTagName(child);
        if (list == null || list.getLength() == 0) return null;
        
        Element childElem = (Element)list.item(0);
        
        // find the text within the child
        Node text = childElem.getFirstChild();
        if (text == null) return null;
        
        return text.getNodeValue();
    
public intgetItemCount()
Get the number of items

        return items.length;
    
public java.lang.StringgetLinkAt(int index)
Get the link for the item with the given index

        return items[index].link;
    
public java.lang.StringgetTitleAt(int index)
Get the title for the item with the given index

        return items[index].title;
    
public voidparse(java.lang.String url)
Parse an RSS file, given its URL

        // connect to the url
        URLConnection uc = (new URL(url)).openConnection();
        
        // parse the XML
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = factory.newDocumentBuilder();
        Document doc = db.parse(uc.getInputStream());
    
        // find the rss element
        Element rss = doc.getDocumentElement();
        
        // parse the channel
        NodeList rssList = rss.getElementsByTagName(CHANNEL_TAG);
        channel = new Item();
        Element channelElem = (Element)rssList.item(0);
        channel.title = getElementValue(channelElem, TITLE_TAG);
        channel.link = getElementValue(channelElem, LINK_TAG);
        
        // get all the items
        NodeList channelList = channelElem.getElementsByTagName(ITEM_TAG);
        items = new Item[channelList.getLength()];
        
        // parse each item
        for (int i = 0; i < channelList.getLength(); i++) {
            Element cElem = (Element)channelList.item(i);
            items[i] = new Item();
            items[i].title = getElementValue(cElem, TITLE_TAG);
            items[i].link = getElementValue(cElem, LINK_TAG);
        }