选择显示字体大小

使用jsr172解析xml

下面我们通过一个例子介绍如何使用jsr 172解析xml,首先我们需要准备一个xml文件放在项目当中,内容如下:

csson>

诺基亚7610
黑色

csson>
值得注意的是当xml文件中包含汉字的时候,我们应该使用文本工具,比如notepad或者ultral edit等把它转换成utf-8编码文件,否则解析的结果将包含乱码。

为了保存xml文件中的信息,我们构造一个普通的java类phone,它包含两个成员变量分别对应name和colour,代码如下所示:
/*
* phone.java
*
* created on 2005年8月6日, 下午9:40
*
* to change this template, choose tools options and locate the template under
* the source creation and management node. right-click the template and choose
* open. you can then make changes to the template in the source editor.
*/

/**
*
* @author administrator
*/
public class phone {

private string colour = "";
private string name = "";
/** creates a new instance of phone */
public phone() {
}

public string getcolour() {
return colour;
}

public void setcolour(string colour) {
this.colour = colour;
}

public string getname() {
return name;
}

public void setname(string name) {
this.name = name;
}

}
jsr 172中实现的是sax模式的解析器,它和dom模式的不同在于,sax解析器按照顺序解析文件并不保存其内容,而dom解析器则是首先把xml文件解析后存储在一个对象树中,可见dom模式更加耗费内存资源。能够解析xml之前首先需要创建saxparser的实例,
saxparserfactory factory = saxparserfactory.newinstance();
saxparser saxparser = factory.newsaxparser();
接下来我们要获得xml文件的输入流,并把它作为其中一个参数传递给saxparser的parse方法,
inputstream is = this.getclass().getresourceasstream("phone.xml");
saxparser.parse(is,new basichandler(this));
那么saxparser是如何解析xml文件的呢?defaulthandler是sax2默认的事件处理器基类,用于处理xml解析事件的方法如下:
startdocument()
startelement(java.lang.string uri, java.lang.string localname, java.lang.string qname, attributes attributes)
characters(char[] ch, int start, int length)
endelement(java.lang.string uri, java.lang.string localname, java.lang.string qname)
enddocument()
默认情况下,defaulthandler的上述方法什么也不做,因此我们必须自己扩展defaulthandler并且覆盖上述的方法。我们的程序中提供了一个basichandler用来处理xml文件。
class basichandler extends defaulthandler
在basichandler类中有两个成员变量
private vector phones = new vector();
private stack tagstack = new stack();
phones用来存储我们已经解析出来的phone对象,tagstack则用来存放我们解析到的元素名称,比如sonyericsson,phone,name,colour等。在文档解释结束后,也就是在enddocument()方法内我们把解析的结果显示在手机屏幕上,为了让读者可以更清楚地明白sax解析器的解析顺序,这里笔者用了一些打印语句来把重要的信息打印出来,basichandler的几个重要方法如下:
public void startdocument() throws saxexception {}

public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception {
system.out.println("the qname is "+qname);
if(qname.equals("phone")) {
phone phone = new phone();
phones.addelement(phone);
}

tagstack.push(qname);
system.out.println("the tag stack's length is "+tagstack.size());
}

public void characters(char[] ch, int start, int length) throws saxexception {
string chars = new string(ch, start, length).trim();
system.out.println("the character is "+chars);

if(chars.length() > 0) {
string qname = (string)tagstack.peek();

phone currentphone = (phone)phones.lastelement();

if (qname.equals("name")) {
currentphone.setname(chars);
} else if(qname.equals("colour")) {
currentphone.setcolour(chars);
}
}
}

public void endelement(string uri, string localname, string qname) throws saxexception {
system.out.println("the end qname is "+qname);
tagstack.pop();
}

public void enddocument() throws saxexception {
stringbuffer result = new stringbuffer();
for (int i=0; i phone currentphone = (phone)phones.elementat(i);
result.append(currentphone.getname() + " 是 " + currentphone.getcolour() + "\n");
}

helloxml.alert(result.tostring());
}

从这里下载
源代码
总结:本文讲述了如何使用jsr 172提供的轻量级xml解析器来解析xml,并给出了具体的代码。下篇文章我们将一起学习一下如何使用web services的远程调用api。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons