Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 10:50, 9 May 2008.

J2ME RSS Parser with KXml

From Forum Nokia Wiki

We'll build a simple RSS parser using KXML library. You can download full source code (including a sample test midlet) with the attached zip file: Media:kxmlrssparser.zip. To see the test midlet in action you can go here.

Image:J2me_kxml_rss_parser.png

RssItem Class

This is the class that will represent a single Rss Item instance. Within this article, we'll only care about title, link and description fields of Rss items, but including other fields will be easy once the process is clear.

public class RssItem
{
	public String title = null;
	public String description = null;
	public String link = null;
	
	public RssItem()
	{
	}
	
	public RssItem(String title, String link, String description)
	{
		this.title = title;
		this.link = link;
		this.description = description;
	}
}

KXmlRssParser Class

This is the class that will actually parse the RSS feed. The only public method will be parse(String rssUrl), accepting the RSS URL as argument, and returning a Vector containing the parsed RssItems.

public Vector parse(String rssUrl) throws Exception
{
	Vector items = new Vector();
	
	KXmlParser parser = new KXmlParser();
 
	HttpConnection conn = (HttpConnection)Connector.open(rssUrl);
	
	InputStream rssStream = conn.openInputStream();
	
	InputStreamReader isr = new InputStreamReader(rssStream);
		
	parser.setInput(isr);
	
	parser.nextTag();
	
	parser.require(XmlPullParser.START_TAG, null, "rss");
	
	parser.nextTag();
	
	parser.require(XmlPullParser.START_TAG, null, "channel");
	
	parser.nextTag();
	
	while(parser.getEventType() != XmlPullParser.END_TAG)
	{
		String nodeName = parser.getName();
		
		if(nodeName.compareTo("item") == 0)
		{
			items.addElement(parseRssItem(parser));
		}
		else
		{
			parser.skipSubTree();
		}
		parser.nextTag();
	}
	isr.close();
		
	rssStream.close();
 
	conn.close();
	
	return items;
}

We have first instantiated our items Vector and KXmlParser. Then we have set the parser input with an InputStream, via an InputStreamReader. Then, we call 2 times the nextTag() method, and check for "rss" and "channel" tags to be present:

parser.nextTag();
	
parser.require(XmlPullParser.START_TAG, null, "rss");
	
parser.nextTag();
	
parser.require(XmlPullParser.START_TAG, null, "channel");

Then, after calling once again nextTag(), we enter a while loop and check for all available "item" tags, discarding all other infos with the skipSubTree() method.

while(parser.getEventType() != XmlPullParser.END_TAG)
{
	String nodeName = parser.getName();
	
	if(nodeName.compareTo("item") == 0)
	{
		items.addElement(parseRssItem(parser));
	}
	else
	{
		parser.skipSubTree();
	}
	parser.nextTag();
}

When we encounter a "item" tag, we parse it with the following parseRssItem() method. This method, as seen before, will parse only title, link and description infos, discarding all other data.

RssItem parseRssItem(KXmlParser parser) throws Exception
{
	RssItem item = new RssItem();
	
	parser.nextTag();
 
	while(parser.getEventType() != XmlPullParser.END_TAG)
	{
		String nodeName = parser.getName();
		
		if(nodeName.compareTo("title") == 0)
		{
			item.title = parser.nextText();
		}
		else if(nodeName.compareTo("description") == 0)
		{
			item.description = parser.nextText();
		}
		else if(nodeName.compareTo("link") == 0)
		{
			item.link = parser.nextText();
		}
		else
		{
			parser.skipSubTree();
		}
		parser.nextTag();
	}
	return item;
}

Finally, we have to close all our open resources: the InputStreamReader, InputStream and Connection instances.

Simple Usage of KXmlRssParser class

We can build simple list of rss item titles with the following code:

public TitleList()
{
	super("Rss Feed", List.IMPLICIT);
	
	KXmlRssParser parser = new KXmlRssParser();
	
	try
	{
		Vector rssItems = parser.parse("http://www.jappit.com/blog/feed/");
		
		for(int i = 0; i < rssItems.size(); i++)
		{
			append(((RssItem)rssItems.elementAt(i)).title, null);
		}
	}
	catch(Exception e)
	{
		append("Error: " + e, null);
		
		e.printStackTrace();
	}
}
Related Discussions
Thread Thread Starter Forum Replies Last Post
SyncML Toolkit symbiannet OMA DM/DS/CP 7 2006-12-24 10:28
Xml::CParser leaks memory?? daveice General Symbian C++ 5 2007-12-04 12:56
Including kxml into Zucotto WHITEboard IDE? diddytee Mobile Java General 0 2002-05-15 08:15
XML parser on carbide.c++ IDE khaliloenit General Symbian C++ 4 2008-03-08 14:32
parser error ryan1629 Symbian Tools & SDKs 1 2005-08-30 08:50
 
Powered by MediaWiki