KXML2 with J2ME
I 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();
}
}
Have u ever used ibotta app for rebate? Try it..Use aydtcuy and you will be added to my team, we can earn fast by making team and generating bonus..Worth it..
© 2009 chirag272003
