Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 06:26, 25 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

KXml XML Parser in Java ME

From Wiki
Jump to: navigation, search
Article Metadata
Series 40
Created: wang_shui (02 Apr 2007)
Last edited: hamishwillee (25 Jul 2013)

This article demonstrates how to use a XML parser in Java ME.

You can use XML parsers in Java ME applications to interface with an existing XML service. For example, you could get a customized view of news on your phone from an aggregator site that summarizes headlines and story descriptions for a news site in XML format.

XML parsers tend to be bulky, with heavy run-time memory requirements. In order to adapt to the MIDP environment, XML parsers must be small to meet the resource constraints of MIDP-based devices. They should also be easily portable, with minimum effort required to port them to MIDP.

Two frequently used XML parsers for resource-constrained devices are kXML and NanoXML.

kXML is a small XML pull parser, specially designed for constrained environments like Java ME platform (CLDC and MIDP).The current version of kXML is KXml2. In contrast to kXML1, it features cursor API instead of event objects, leading to a reduced footprint and less object creation overhead.

As of version 1.6.8 for MIDP, NanoXML supports DOM parsing.

There is also XML parser in the Web Services API (JSR-172), which can be used instead of the above mentioned parsers, which needs to be included in every MIDlet suite trying to use them. The XML parsing package has been implemented on S60 3rd Edition devices (and newer) as well as on Series 40 3rd Edition Feature Pack 1 devices (and newer).

Using KXml

Download the latest version of KXml2 here. Include the jar to your project lib.

Let us take a simple XML to be parsed.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<txt>Capital of India is</txt>
<A>New Delhi</A>
<txt>India got independence in</txt>
import java.util.Vector;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
* Parses the XML file
* @author shivam

public class Parser {
* Kxml parser instance

private KXmlParser xmlParser = null;
* singleton instance of Parser class

private static Parser instance = null;
//private Vector questionSet = null;
/** Creates a new instance of Parser */
private Parser() {
xmlParser = new KXmlParser();
questionSet = new Vector(Utility.TOTAL_QUESTIONS);
* get the instance of this class
* @return instance

public static Parser getInstance()
if(instance == null)
instance = new Parser();
return instance;
* Parses the XML file
* @throws error parsing

public void getQuestions() throws IOException
xmlParser.setInput(new ReadFile().gtFileStream(Utility.QUESTION_FILE_PATH));
xmlParser.require(XmlPullParser.START_TAG, null,Utility.TAG_QUESTION_SET);
while (xmlParser.nextTag () != XmlPullParser.END_TAG)
xmlParser.require(XmlPullParser.END_TAG, null, Utility.TAG_QUESTION_SET);;
xmlParser.require(XmlPullParser.END_DOCUMENT, null, null);
catch(Exception ioe)
throw new IOException(ioe.getMessage());
* Reads the XML data in a buffer
* @param parser kxml parser object
* @return Question
* @throws error reading data
* @throws org.xmlpull.v1.XmlPullParserException parser exception

private Question readXMLData(KXmlParser parser) throws IOException, XmlPullParserException {
//Parse our XML file
parser.require(XmlPullParser.START_TAG, null, Utility.TAG_QUESTION);
Question quest = new Question();
while (parser.nextTag() != XmlPullParser.END_TAG) {
parser.require(XmlPullParser.START_TAG, null, null);
String name = parser.getName();
String text = parser.nextText();
System.out.println ("<"+name+">"+text);
if (name.equals(Utility.TAG_QUESTION_DESCRIPTION))
else if (name.equals(Utility.TAG_QUESTION_ANSWER))
else if (name.equals(Utility.TAG_QUESTION_OPTION_A))
else if (name.equals(Utility.TAG_QUESTION_OPTION_B))
else if (name.equals(Utility.TAG_QUESTION_OPTION_C))
else if (name.equals(Utility.TAG_QUESTION_OPTION_D))
parser.require(XmlPullParser.END_TAG, null, name);
parser.require(XmlPullParser.END_TAG, null, Utility.TAG_QUESTION);
return quest;

Helpful articles:

This page was last modified on 25 July 2013, at 06:26.
87 page views in the last 30 days.