A basic Jdom parser for RSS

128px-feed-iconsvg

Almost two years back I posted a SAX based RSS parser (find it here) which was intented for J2ME. But we have JDOM parser which I think is a lot easier than SAX. In this post you can find a very simple JDOM based RSS parser.

Know more about RSS here: http://en.wikipedia.org/wiki/RSS_(file_format)

Step1: Import the JDOM libraries

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;


Step 2:
Initialize

// I have not given the implementation of
// getUrlConnectionInputStream(url)
inputStream = getUrlConnectionInputStream(url);
if(null == inputStream)
   throw new Exception("No input stream for " + url);

saxBuilder = new SAXBuilder();
document = saxBuilder.build(inputStream);
rssFeed = _build(document);


Step 3:
Implementation of _build(org.jdom.Document)

// Entry point. Returns a RssFeed object corresponding
// to the given RSS feed URL
private RssFeed _build(Document document)
throws Exception
{
  RssFeed rssFeed = null;
  Element rootElement = null;
  Element channelElement = null;
  String  rssFeedVersion = null;

  if (null == document)
    throw new Exception("Empty document");

  rootElement = document.getRootElement();
  if (!"rss".equalsIgnoreCase(rootElement.getName()))
    throw new Exception("Invalid XML");

  rssFeedVersion = rootElement.getAttributeValue("version");
  channelElement = rootElement.getChild("channel");
  if (null == channelElement)
    throw new Exception("Empty feed");

  // Getting the feed contents
  rssFeed = _getHeader(channelElement);
  rssFeed.version = rssFeedVersion;
  _addFeedItems(channelElement, rssFeed);

  return (rssFeed);
}

Step 4: _getHeader(org.jdom.Element)

This method reads the feed header and sets the values to the RssFeed object.

// Sets the RSS feed heder information to the
// RssFeed object
rssFeed = new RssFeed();
rssFeed.title = getValueOfChildElement(channelElement, "title");
rssFeed.link = getValueOfChildElement(channelElement, "link");
rssFeed.description = getValueOfChildElement(channelElement, "description");


Step 5:
_addFeedItems(org.jdom.Elements,  subin.xml.RssFeed)

This method extracts each feed item from the XML and adds it to the given RssFeed object.

// Iterates through the feed item list, extracts the
// feed item details, creates corresponding RssItem object
// and adds it to the RssFeed item list
java.util.List<element> itemElements = null;
RssItem anRssItem = null;

itemElements = channelElement.getChildren("item");
if (null != itemElements)
{
  for (Element anItemElement : itemElements)
  {
    anRssItem = new RssItem();
    anRssItem.title = getValueOfChildElement(anItemElement, "title");
    anRssItem.link = getValueOfChildElement(anItemElement, "link");
    anRssItem.description = getValueOfChildElement(anItemElement, "description");
    anRssItem.pubDate = getValueOfChildElement(anItemElement, "pubDate");
    rssFeed.addItem(anRssItem);
  }
}

Step 6: getValueOfChildElement(org.jdom.Element, String)

This method extract the value of the child node (specified by the name) from the given JDOM Element.

// Get the child node value
private String getValueOfChildElement(Element parentElement,
  String tagName)
{
  Element childElement = null;
  String  tagValue     = null;
  childElement = parentElement.getChild(tagName);
  tagValue = (null != childElement)
    ? childElement.getValue().trim() : null;
  return (tagValue);
}

Step 7: RssFeed & RssItem

Two classes to hold the feed information.

class RssFeed
{
  public  String version;
  public  String title;
  public  String description;
  public  String link;

  public List <rssitem> items;
}
class RssItem
{
  public  String title;
  public  String description;
  public  String link;
  public  String pubDate;
}

If you find it difficult to follow as it is not a single file, I’m very sorry. But I hope this will be useful.

Advertisements