import java.io.*;
import java.util.*;
import java.lang.String;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
//下面两个包出现在XML分析程序tinyTreeDemo中
import gd.xml.*;
import gd.xml.tiny.*;
public class tinyTreeDemo extends
MIDlet implements CommandListener
{
private String url;
private ParsedXML root;
private Display myDisplay=null;
private Form mainScreen;
private TextField requestField;
Command sendCommand=new Command
("SEND",Command.OK,1);
public tinyTreeDemo()
{
//放置mail.xml文件的Web站点
url="http://127.0.0.1:8000/mail.xml";
myDisplay=Display.getDisplay(this);
mainScreen=new Form("Type a URL:");
requestField=new TextField
(null,url,100,TextField.URL);
mainScreen.append(requestField);
mainScreen.addCommand(sendCommand);
mainScreen.setCommandListener(this);
}
public void startApp()
throws MIDletStateChangeException
{
myDisplay.setCurrent(mainScreen);
}
public void pauseApp()
{
}
public void destroyApp
(boolean unconditional)
{
}
public void commandAction
(Command c,Displayable s)
{
if(c==sendCommand)
{
String urlString=
requestField.getString();
try
{
//返回XML根元素
root=TinyParser.parseXML(url);
displayNode(root);
}
catch(ParseException e)
{
System.err.println("startApp:" + e);
}
}
}
private void displayNode
(ParsedXML px)
{
//返回节点对象类型
String nodeName=px.getTypeName();
//返回节点对象类型+名称,例如tag<mail>,
tag为类型(标签),mail为节点名
if(px.getName()!=null)
nodeName+="<" + px.getName() + ">";
//返回存储在标签之间的内容
String nodeContent=px.getContent();
if(nodeContent==null)
nodeContent="";
//在控制台中打印出来
System.out.println(nodeName + ":");
System.out.println(nodeContent);
Enumeration e;
//返回属性,如果有就存储在Enumeration中
e=px.attributes();
if(e.hasMoreElements())
{
System.out.print("attribute:");
while(e.hasMoreElements())
{
//返回属性名称
String attrName=(String)e.nextElement();
//px.getAttribute(attrName)返回属性的值
System.out.println(attrName +
":" + px.getAttribute(attrName));
}
}
//返回节点中的元素,
如果有就存储在Enumeration中
e=px.elements();
if(e.hasMoreElements())
{
//显示下一个节点
while(e.hasMoreElements())
displayNode(
(ParsedXML)e.nextElement());
}
}
} |