KXML2 with J2ME
85I will cover how to use kxml 2 parser with J2ME midlet. I will assume that you are aware with xml and J2ME.
I will explain kxml and then will show example of kxml with J2ME.
Basically kxml 2 implements the XMLPull Parser API. Pull parser is useful when require fast and a small XML parser.
It should be used when all the process has to be performed quickly and efficiently to input elements. We can say kxml is the J2ME Parser.
Now I will show you example of parsing simple xml using kxml.
Test.xml
<order>
<book>Maths</book>
<book price="5">English</book>
</order>
J2ME Implementation using kxml parser for parsing above xml:
try {
File file = new File("C:\\TmintNewCodeTree\\QuartzSample\\resources\\Test1.xml");
InputStream in = new FileInputStream(file);
InputStreamReader is = new InputStreamReader(in);
KXmlParser parser = new KXmlParser();
parser.setInput(is);
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, "order");
while (parser.next() != XmlPullParser.END_DOCUMENT) { //parser.nextTag()
if (parser.getEventType() == XmlPullParser.START_TAG) {
System.out.println("start tag: " + parser.getName()+" Att count: "
+ parser.getAttributeCount() + "Book name:"+parser.nextText());
if(parser.getAttributeCount()>0){
System.out.println(" Value:"+parser.getAttributeValue(0));
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
In above implementation we are iterating xml file till last tag. Then we are checking
if current tag is start tag then do something.
next() or nextTag() advances to next tag. nextTag() advances to the next start or end tag,
skipping insignificant events such as white space, comments.
nextText() requires that the current position is a start tag. It returns the text content of the
corresponding element. The post condition for nextText() is that the current position is an end tag.
public class KXMLSample extends MIDlet {
KXmlParser parser = null;
public void startApp() {
InputStream in = this.getClass().getResourceAsStream("//order.xml");
InputStreamReader is = new InputStreamReader(in);
parser = new KXmlParser();
try {
parser.setInput(is);
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, "order");
while (parser.next() != XmlPullParser.END_DOCUMENT) {
if (parser.getEventType() == XmlPullParser.START_TAG) {
System.out.println("start tag: " + parser.getName()+" Att count: "
+ parser.getAttributeCount() + parser.nextText());
if(parser.getAttributeCount()>0){
System.out.println("Attr Name:"+parser.getAttributeName(0)+" Value: "+parser.getAttributeValue(0));
}
}
}
} catch (XmlPullParserException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
protected void pauseApp(){}
protected void destroyApp(boolean unconditional){
notifyDestroyed();
}
}PrintShare it! — Rate it: up down flag this hub
Comments
It may be possible that your mobile is not supporting KXML or xml file that you specified is not found at given location.
this is not first time that i use kxml in my mobile!
before i used from kxml1.2.1 in my mobile and it worked good!
i used from this line in my code to reading xml file
InputStream in = this.getClass().getResourceAsStream("//order.xml");
mobile model:6120 classic (OS:Symbian9.2)
Can you please paste whole error stack trace that you get? So that i can give you more precise answer. One more thing your order.xml should be inside resources folder.
Thanks.











farshid says:
3 months ago
when i transfer and install it into my mobile phone then i receive an unhand led exception!!
just i compile and move it!