选择显示字体大小

用digester简化xml文档处理(一)

  digester 框架属于 jakarta commons,它以规则和模式为基础处理xml文档。与 sax 和 dom 之类的标准api相比,digester 不涉及太多的细节问题,非常适合于对xml文档进行简单的处理。

  在javaxml开发中,一个常见的任务是把xml文档转换成对应的java bean对象的层次结构。人们经常用标准的sax和dom api来完成这个任务。虽然这两种api都很强大和灵活,但对于某些简单的任务来说,它们显得操作层次太低,也就是说,涉及了太多的细节问题。jakarta digester框架能够很好地满足这类场合的需要。


  digester框架简介

  jakarta的digester框架struts框架发展而来,原先被用来处理struts-config.xml配置文件,但很快人们认识到它有着更广泛的用途,把它转入了jakarta commons项目。jakarta commons的目标是提供一个“可重用java组件的仓库”。digester最新的版本是1.3,于2002年8月13日发布。

  digester框架允许开发者指定一组动作,当解析器在xml文档中发现某些特定的简单模式时动作被执行。digester框架带有10个预定义的规则(rule),涵盖了unmarshalling xml(例如创建bean或设置bean属性)的大多数需求( marshalling的原意是指“配制整齐,编组列车”,marshalling是在内存中为java对象生成xml描述文档的过程,而unmarshalling是指把xml形式的描述转换到可用java代码操作的对象的过程,我们称之为“反配制”),但必要时用户可以定义和实现自己的规则。

  在本文的例子中,我们将反配制下面这个xml文档:

<?xml version=&quot;1.0&quot;?>
<catalog library=&quot;somewhere&quot;>
<book>
<author>author 1</author>
<title>title 1</title>
</book>
<book>
<author>author 2</author>
<title>his one book</title>
</book>
<magazine>
<name>mag title 1</name>
<article page=&quot;5&quot;>
<headline>some headline</headline>
</article>
<article page=&quot;9&quot;>
<headline>another headline</headline>
</article>
</magazine>
<book>
<author>author 2</author>
<title>his other book</title>
</book>
<magazine>
<name>mag title 2</name>
<article page=&quot;17&quot;>
<headline>second headline</headline>
</article>
</magazine>
</catalog>

  下面是bean的代码。注意使用digester框架时,bean类必须定义成public。

import java.util.vector;
public class catalog {
private vector books;
private vector magazines;
public catalog() {
books = new vector();
magazines = new vector();
}
public void addbook( book rhs ) {
books.addelement( rhs );
}
public void addmagazine( magazine rhs ) {
magazines.addelement( rhs );
}
public string tostring() {
string newline = system.getproperty( &quot;line.separator&quot; );
stringbuffer buf = new stringbuffer();
buf.append( &quot;--- books ---&quot; ).append( newline );
for( int i=0; i<books.size(); i++ ){
buf.append( books.elementat(i) ).append( newline );
}
buf.append( &quot;--- magazines ---&quot; ).append( newline );
for( int i=0; i<magazines.size(); i++ ){
buf.append( magazines.elementat(i) ).append( newline );
}
return buf.tostring();
}
}
//===================================================
public class book {
private string author;
private string title;
public book() {}
public void setauthor( string rhs ) { author = rhs; }
public void settitle( string rhs ) { title = rhs; }
public string tostring() {
return &quot;book: author='&quot; + author + &quot;' title='&quot; + title + &quot;'&quot;;
}
}
//===================================================
import java.util.vector;
public class magazine {
private string name;
private vector articles;
public magazine() {
articles = new vector();
}
public void setname( string rhs ) { name = rhs; }
public void addarticle( article a ) {
articles.addelement( a );
}
public string tostring() {
stringbuffer buf = new stringbuffer( &quot;magazine: name='&quot; + name + &quot;' &quot;);
for( int i=0; i<articles.size(); i++ ){
buf.append( articles.elementat(i).tostring() );
}
return buf.tostring();
}
}
//===================================================
public class article {
private string headline;
private string page;
public article() {}
public void setheadline( string rhs ) { headline = rhs; }
public void setpage( string rhs ) { page = rhs; }
public string tostring() {
return &quot;article: headline='&quot; + headline + &quot;' on page='&quot; + page + &quot;' &quot;;
}
}


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

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