You Are Here:

Community: Wiki

This page was last modified 09:10, 8 June 2009.

J2ME RSS Parser with KXml

From Forum Nokia Wiki

We'll build a simple RSS parser using the KXML library. You can download the full source code (including a sample test midlet). To see the test midlet in action you can go to 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 the 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 feed's URL as the 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();
	}
}

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditFurlTechnocratiMagnoliaTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fMMPE5ffileX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxMMPE20fileE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfnTypeZCommunityContentQ qdcZtypeQUqfnTypeZE52esourceQ qdcZtypeQUqfnTypeZWebpageQ qdcZtypeQUqfnTypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZE52esourceQ qfnZtypeQUqfnTypeZWebpageQ qfnZtypeQUqfnTypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d02X qfnZuserE5ftagQSxfileX qfnZuserE5ftagQSxlibpathX qfnZuserE5ftagQSxmmpX qfnZuserE5ftagQSxresourceX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfnTypeZCommunityContentQ qrdfZtypeQUqfnTypeZE52esourceQ qrdfZtypeQUqfnTypeZWebpageQ qrdfZtypeQUqfnTypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx2E2E0000X