Methods Summary |
---|
public java.lang.String | getChannelLink()Get the link associated with the channel
return channel.link;
|
public java.lang.String | getChannelTitle()Get the title of the channel
return channel.title;
|
private java.lang.String | getElementValue(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 int | getItemCount()Get the number of items
return items.length;
|
public java.lang.String | getLinkAt(int index)Get the link for the item with the given index
return items[index].link;
|
public java.lang.String | getTitleAt(int index)Get the title for the item with the given index
return items[index].title;
|
public void | parse(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);
}
|