|
jakarta commons digester是目前流行的、开放源码的xml文件处理实用软件包。本文简要介绍了digester的功能,并且以一个程序来示范如何利用该软件简化原本复杂的xml配置文件的解析过程。
apache小组的jakarta项目下有许多知名的基于java的开放源码子项目,包括tomcat,ant,log4j,struts等等。相比之下,jakarta commons子项目在它们当中则属于知名度比较低的。该项目致力于提供可重用的java构件,例如:commons beanutils, commons dbcp 以及commons logging等。利用它们可以大大减少的一些乏味的重复编码工作。本文将重点介绍commons digester,它具有把xml文件映射到java对象的能力。也许恰好她能解决你目前的问题呢!那么,请继续往下看吧。
需要注意的是:在启用digester之前,你的classpath下必须已经安装了这些软件包:digester ,beanutils, collections, logging,还有一个遵循sax(simple api for xml)2.0或jaxp(java api for xml parsing) 1.1规范的xml解析器。 有关jakarta commons所有的构件,以及两个合适的解析器crimson和xerces的链接可以在本文的相关资源中找到。
xml解析简介
总的来说,xml文件解析有2种基本方法。一种是dom(document object model)方法。采用dom方法进行解析时,xml解析器读取整个xml文件并生成一个树状表达方式。另一个是使用sax,它的特点是不需要遍历整个文件而是用事件驱动来解析文件。虽然dom方法有时实现起来比较容易,但是与sax方法相比速度更慢而且更耗资源。digester构件通过为sax事件提供高级的接口大大简化了sax方法的解析过程。该接口隐含了大量xml文件处理的复杂的技术细节。使开发人员得以集中精力处理xml数据而不是花太多的时间在如何解析文件本身上。
digester的几个重要概念
digester引入了3个重要概念:元素匹配模式,处理规则以及对象栈。
元素匹配模式将处理规则与xml元素联系起来。以下是一个xml层次结构的元素匹配模式的例子。
<datasources>
'datasources'
<datasource> 'datasources/datasource'
<name/> 'datasources/datasource/name'
<driver/> 'datasources/datasource/driver'
</datasource>
<datasource> 'datasources/datasource'
<name/> 'datasources/datasource/name'
<driver/> 'datasources/datasource/driver'
</datasource>
</datasources>
|
每当发现一个匹配的模式,对应的处理规则就被激活。在上面的示例中,与'datasource/datasource'相关联的规则将会运行2次。
处理规则定义了发现匹配的模式时产生的行为。digester模块已经预定了不少处理规则,自定义的处理规则可以通过扩展 org.apache.commons.digester.rule类来实现。
对象栈用来保存处理规则所要处理的对象。对象可以由人为或处理规则压入和弹出对象栈。
使用digester
digester 时常用于解析xml配置文件,下面例子中的xml文件包含了用于创建datasource池的配置信息。 其中datasource是抽象类,拥有一个空的构造函数,多个get,set方法来存取字符串。
<?xml version="1.0"?>
<datasources>
<datasource>
<name>hsqldatasource</name>
<driver>org.hsqldb.jdbcdriver</driver>
<url>jdbc:hsqldb:hsql://localhost</url>
<username>sa</username>
<password></password>
</datasource>
<datasource>
<name>oracledatasource</name>
<driver>oracle.jdbc.driver.oracledriver</driver>
<url>jdbc:oracle:thin:@localhost:1521:orcl</url>
<username>scott</username>
<password>tiger</password>
</datasource>
</datasources>
|
要使用digester对之进行解析,首先我们要创建一个digester类的实例,然后把所需的对象都压入到digester对象栈中,设置一系列处理规则,最后,对文件进行解析。下面是一个例子:
digester digester = new digester();
digester.addobjectcreate("datasources/datasource",
"datasource");
digester.addcallmethod("datasources/datasource/name","setname",0);
digester.addcallmethod("datasources/datasource/driver","setdriver", 0);
digester.parse("datasource.xml");
|
在这个例子中,你可以看到,addobjectcreate()方法把一个objectcreaterule添加到'datasources/datasource'模式中。这样,objectcreaterule将创建一个新的datasource类的实例并把它压入digester的对象栈中。随后,addcallmethod()方法把callmethodrule分别添加到两个模式中。结果是,callmethodrule将调用位于对象栈顶部的对象的特定的方法,addcallmethod()的最后一个参数指定了传入该方法的参数数目,如果数目为0则匹配上的数据项内容将作为参数传入。(注:类objectcreaterule,callmethodrule 都是digester模块已定义的处理规则)
假如应用这个代码断来处理在前面提到的xml文件,将会发生以下结果:
· 一个新的datasource类的实例将被创建并压入堆栈;
· 调用该实例的setname(string name)方法,参数值为'hsqldatasource';
· 调用该实例的setdriver(string driver)方法,参数值为'oracledatasource'。
在对'datasource'元素的处理结束后,对象从堆栈中弹出,文件剩余部分的解析重复以上过程。上面的例子存在一个问题,在对相关元素的处理结束后,objectcreaterule会弹出它创建的对象。因此,当digester完成解析文件后,只有最后一次创建的对象被保留下来。解决这个问题也很容易,只要把对象在解析开始之前压入堆栈,随后调用该对象的某个方法来创建所需的任意对象。下面的类就是这个办法的一个演示:
public class sampledigester
{
public void run() throws ioexception, saxexception
{
digester digester = new digester();
// this method pushes this (sampledigester) class to the digesters
// object stack making its method s available to processing rules.
digester.push(this);
// this set of rules calls the adddatasource method and passes
// in five parameters to the method.
digester.addcallmethod ("datasources/datasource", "adddatasource", 5);
digester.addcallparam("datasources/datasource/name", 0);
digester.addcallparam("datasources/datasource/driver", 1);
digester.addcallparam("datasources/datasource/url", 2);
digester.addcallparam("datasources/datasource/username", 3);
digester.addcallparam("datasources/datasource/password", 4);
// this method starts the parsing of the document.
digester.parse("datasource.xml");
}
// example method called by digester.
public void adddatasource(string name,
string driver,
string url,
string username,
string password)
{
// create datasource and add to collection...
}
}
|
在上面的sampledigester类中,每当匹配到 'datasources/datasource'时都会自动调用adddatasource()方法。addcallparam()方法将匹配到的数据项内容作为传入adddatasource()方法的参数。在adddatasource()方法中,你就可以创建具体的datasouce并把它添加入你的datasource集合中去。
深入了解digester
尽管digester最初是为了简化对xml配置文件的解析而开发的。但是它也可以用在把xml文件映射到java对象的场合。本文只是对digester的简要介绍。如果要深入了解digester或jakarta commons 项目中的其他公共组件,可以访问jakarta commons的官方网站。另外,可以参考以下的相关资源,从这些开放源代码的项目应用中学习digester的用法。
相关资源
·the jakarta commons homepage: http://jakarta.apache.org/commons
·two open source xml parsers suitable for use with digester are:
o xerces
o crimson
·open source projects that use digester:
o struts
o tomcat
·other jakarta commons components:
http://jakarta.apache.org/commons/components.html
· erik swenson's previous open source profile column, "reports made easy with jasperreports" (javaworld, september 2002):
http://www.javaworld.com/javaworld/jw-09-2002/jw-0920-opensourceprofile.html
· browse the java and xml section of javaworld's topical index:
http://www.javaworld.com/channel_content/jw-xml-index.shtml
·chat about java development in the javaworld forum:
http://forums.devworld.com/webx?13@@.ee6b802
· sign up for javaworld's free weekly email newsletters:
http://www.javaworld.com/subscribe
· you'll find a wealth of it-related articles from
our sister publications at idg.net
|