Tagged: kxml RSS

  • pit 10:48 am on May 15, 2008 Permalink | Reply
    Tags: , kxml, , xml parsing   

    How to parse a generic XML file in J2ME with kXML 

    Some days ago I’ve posted J2ME code to parse RSS feeds using kXML library. Today’s code is about parsing a generic XML, so you can use it to parse any XML document you want. Code is splitted in the following two classes.

    XmlNode class

    This is the class representing a single node. We’ll define 2 node types:

    • Text nodes: nodes without a name, contanining only a textual value. For them we’ll define a TEXT_NODE final variable
    • Element nodes: nodes with a tag name, that can have children nodes and/or attributes

    XmlNode class source code is available here: XmlNode.java

    GenericXmlParser class

    This is the class that will actually do XML parsing. We’ll define only one public method, parseXML(), that will accept 2 arguments:

    • a KXmlParser instance, that will be used to do XML parsing
    • a boolean that will tell if whitespace-only text nodes must be ignored or not

    and will return and XmlNode representing the root node of the parsed XML tree.

    GenericXmlParser class source code is available here: GenericXmlParser.java

    Sample usage

    Let’s see how we can use the two classes above. First we must instantiate and initialize a KXmlParser, and then we can call our GenericXmlParser parseXML() method.

    InputStreamReader reader = new InputStreamReader(getClass().getResourceAsStream("/test3.xml"));
     
    KXmlParser parser = new KXmlParser();
     
    parser.setInput(reader);
     
    GenericXmlParser gParser = new GenericXmlParser();
     
    XmlNode xml = gParser.parseXML(parser, true);

    Now, we have our resulting XmlNode that will hold the whole XML tree. We can make a simple dump of it to check if it’s all ok, using this test function:

    void dumpXML(XmlNode node, int deep)
    {
    	for(int i = 0; i < deep; i++)
    	{
    		System.out.print(" ");
    	}
    	System.out.print(node.nodeName + " - ");
     
    	if(node.nodeValue != null)
    	{
    		System.out.print("(" + node.nodeValue + ") - ");
    	}
    	String[] attributes = node.getAttributeNames();
     
    	for(int i = 0; i < attributes.length; i++)
    	{
    		System.out.print(attributes[i] + ": " + node.getAttribute(attributes[i]) + ", ");
    	}
     
    	System.out.println();
     
    	for(int i = 0; i < node.children.size(); i++)
    	{
    		dumpXML((XmlNode)node.children.elementAt(i), deep + 1);
    	}
    }

    Note: This article is available also on Forum Nokia Wiki: How to parse an XML file in J2ME with kXML

     
  • pit 1:28 pm on May 9, 2008 Permalink | Reply
    Tags: , kxml, parser, rss,   

    Parsing RSS feeds with J2ME and KXML 

    Some days have passed since last J2ME tutorial, so here is a fresh new one!

    Today we’ll see how parsing a RSS feed with J2me is easy using KXML library, a fast and small XML pull parser, expecially suited for constrained environments like mobile devices. A live sample, parsing the RSS feed of this blog, is available here.

    J2ME Kxml rss parser screenshot

    The detailed explanation of source code is available on my Forum Nokia Wiki article: J2ME RSS Parser with KXml. If you’re interested only in plain source code, you can pick it up here (it includes also the sample midlet you find on the emulator page). To use KXmlRssParser class, you must simply do:

    KXmlRssParser parser = new KXmlRssParser();
     
    Vector rssItems = parser.parse(yourFeedURL);

    and the parse() method will return the complete list of parsed Items, as instances of RssItem class. Source code is of course simplified, for the purpose of this tutorial, as it only considers title, link and description tags of each <item>, but once you understand KXml logic you can extend it, without much effort, to include other infos from RSS feed.

    Other resources you might find useful:

     
    • Mahesh 2:31 pm on June 19, 2008 Permalink

      Hi i m a trainer in my comp, i saw the code its very nice n the standards are maintained i this coding. Currently i m working on xml parsing, so i juz want to know that if i want to display the names(channel names) in a list view on device, then is this the same procedure i need to follow or else there is a diferent way. If so can u plz tell me?

    • Pit 12:23 pm on June 20, 2008 Permalink

      Hi Mahesh,

      the approach and code described in this post works only to parse (and display) RSS feed items, but you can easily adapt it to parse channel names, or any other infos contained within the RSS feed.

      Pit

c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel

Switch to our mobile site