Android Dev: Reading a RSS Feed (updated)

{lang: 'en-GB'}

UPDATE: I have cleaned up the parse() method and added more comments to try and help people. This code will not work for every RSS feed, changes will be needed to get different information from other feeds. It is just an example of how to parse

I have been toying with the idea of trying to reed an RSS feed in an Android app for quite a while.

Every time I looked for a tutorial I was greeted by 10 different classes, and 200 lines of code. Not to mention the different ways to read (SAX, DOM, XPATH etc). I finally stumbled across a short piece of code that I though could never work and just did.

To make it easier to follow my code I separated the code into a few classes:

main.java: My main application class
rssparser.java: The actual parse method
arrays.java: A class to hold the arrays

The code below reads a wordpress RSS feed (the magnificent Epileptic Gibbon Podcast) and parses the information into arrays.

rssparser.java

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class rssparser
{

	public static void parse()
	{
		URL url;
		try
		{
			// Set the url (you will need to change this to your RSS URL
			url = new URL(
					"http://feeds.feedburner.com/TheEpilepticGibbonPodcastMusicShow");

			// Setup the connection
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			// Connect
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK)
			{

				// Retreive the XML from the URL
				DocumentBuilderFactory dbf = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document doc;
				doc = db.parse(url.openStream());
				doc.getDocumentElement().normalize();

				// This is the root node of each section you want to parse
				NodeList itemLst = doc.getElementsByTagName("item");

				// This sets up some arrays to hold the data parsed
				arrays.PodcastTitle = new String[itemLst.getLength()];
				arrays.PodcastURL = new String[itemLst.getLength()];
				arrays.PodcastContent = new String[itemLst.getLength()];
				arrays.PodcastMedia = new String[itemLst.getLength()];

				// Loop through the XML passing the data to the arrays
				for (int i = 0; i < itemLst.getLength(); i++)
				{

					Node item = itemLst.item(i);
					if (item.getNodeType() == Node.ELEMENT_NODE)
					{
						Element ielem = (Element) item;

						// This section gets the elements from the XML
						// that we want to use you will need to add
						// and remove elements that you want / don't want
						NodeList title = ielem.getElementsByTagName("title");
						NodeList link = ielem.getElementsByTagName("link");
						NodeList content = ielem
								.getElementsByTagName("content:encoded");
						NodeList media = ielem
								.getElementsByTagName("media:content");

						// This is an attribute of an element so I create
						// a string to make it easier to use
						String mediaurl = media.item(0).getAttributes()
								.getNamedItem("url").getNodeValue();

						// This section adds an entry to the arrays with the
						// data retrieved from above. I have surrounded each
						// with try/catch just incase the element does not
						// exist
						try
						{
							arrays.PodcastTitle[i] = title.item(0)
									.getChildNodes().item(0).getNodeValue();
						} catch (NullPointerException e)
						{
							e.printStackTrace();
						}

						try
						{
							arrays.PodcastURL[i] = link.item(0).getChildNodes()
									.item(0).getNodeValue();
						} catch (NullPointerException e)
						{
							e.printStackTrace();
						}
						try
						{
							arrays.PodcastContent[i] = content.item(0)
									.getChildNodes().item(0).getNodeValue();
						} catch (NullPointerException e)
						{
							e.printStackTrace();
						}

						try
						{
							arrays.PodcastMedia[i] = mediaurl;
						} catch (NullPointerException e)
						{
							e.printStackTrace();
						}
					}
				}
			}

		} catch (MalformedURLException e)
		{
			e.printStackTrace();
		} catch (DOMException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (ParserConfigurationException e)
		{
			e.printStackTrace();
		} catch (SAXException e)
		{
			e.printStackTrace();
		}

	}

}

Lines to note:

27: Change the URL string to be the RSS URL you want to parse
48-51: These are the arrays I have setup in my arrays.java
65-69: This code looks through the xml to find the tags specified
74: This demonstrates how to read an attribute (for example )
81-113: The data read is passed into the specified arrays

arrays.java


public class arrays {
	
	public static String[] PodcastTitle;
	public static String[] PodcastURL;
	public static String[] PodcastContent;
	public static String[] PodcastMedia;
	

}

The above code just sets up the 4 arrays I wanted.

I really hope this helps somebody, if only I could remember which site I got my original code from I would link back.

…. oops, almost forgot. To run from your main class simply call:

rssparser.parse();

Google+ Comments

Comments are closed.