📄 parsemidlet.java
字号:
package com.j2medev.chapter10;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.xml.parsers.*;
import org.xml.sax.SAXException;
public class ParseMIDlet extends MIDlet implements CommandListener,Runnable{
private Display display = null;
private Form form = null;
private Thread thread = null;
private Command parseCommand = new Command("parse",Command.OK,1);
public void startApp() {
if(display == null){
display = Display.getDisplay(this);
form = new Form("jaxp");
//把document.xml的原始内容显示在Form上
InputStream is = getClass().getResourceAsStream("/document.xml");
try {
byte[] data = new byte[is.available()];
is.read(data);
form.append(new String(data));
//正确显示出来才添加parseCommand
form.addCommand(parseCommand);
} catch (IOException ex) {
ex.printStackTrace();
form.append(ex.getMessage());
}
form.addCommand(new Command("exit",Command.EXIT,1));
form.setCommandListener(this);
}
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable displayable) {
if(command.getCommandType() == Command.EXIT){
destroyApp(false);
notifyDestroyed();
}else if(command == parseCommand){
//开始解析,在单独的线程中完成。
thread = new Thread(this);
thread.start();
}
}
public void run(){
form.deleteAll();
form.append("parsing the xml file");
//创建SAXParserFactory实例
SAXParserFactory instance = SAXParserFactory.newInstance();
try {
//创建SAXParser
SAXParser parser = instance.newSAXParser();
InputStream is = getClass().getResourceAsStream("/document.xml");
//开始解析
parser.parse(is,new MyHandler(form));
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
} catch (SAXException ex) {
ex.printStackTrace();
}catch(IOException ex){
ex.printStackTrace();
}
form.removeCommand(parseCommand);
display.setCurrent(form);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -