×
Namespaces

Variants
Actions
Revision as of 12:10, 8 June 2009 by tanjaluodes (Talk | contribs)

RSS Parser with KXML in Java ME

From Nokia Developer Wiki
Jump to: navigation, search

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.

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();
}
}
152 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×