前端之家收集整理的这篇文章主要介绍了
使用kxml解析xml,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.lc.xml;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import org.kxml.Xml;
import org.kxml.parser.ParseEvent;
import org.kxml.parser.XmlParser;
public class MIDlet1XML extends MIDlet {
private Form form = new Form("xml");
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
Display.getDisplay(this).setCurrent(form);
try {
XmlParser parser = new XmlParser(new InputStreamReader(this
.getClass().getResourceAsStream("/jdom.xml")));
boolean parsing = true;
while (parsing) {
ParseEvent event = parser.read();
switch (event.getType()) {
case Xml.START_TAG:
if ("contacts".equals(event.getName())) {
System.out.println("contacts");
} else if ("root".equals(event.getName())) {
System.out.println("root");
} else if ("send".equals(event.getName())) {
System.out.println("send");
} else if ("receive".equals(event.getName())) {
System.out.println("receive");
} else if ("contact".equals(event.getName())) {
System.out.println("contact");
}
break;
case Xml.END_TAG:
if ("contacts".equals(event.getName())) {
System.out.println("contacts");
} else if ("root".equals(event.getName())) {
System.out.println("root");
} else if ("send".equals(event.getName())) {
System.out.println("send");
} else if ("receive".equals(event.getName())) {
System.out.println("receive");
} else if ("contact".equals(event.getName())) {
System.out.println("contact");
}
break;
case Xml.END_DOCUMENT:
parsing = false;
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}