开源XML处理包:Digester

 

 

 

 

一、Digester简介
Jakarta Commons Digester是Apache小组的Jakarta项目下的子项目,是目前比较流行的、开源的XML文件处理包。目前最新版本是2.0版本。
许多应用都需要处理XML格式的数据,这时Digester是个很好的选择。Digeste提供事件驱动管理器处理XML文件。开发者可以使用熟悉简单的API,以SAX方式解析XML。提供开发友好的SAX事件接口,使开发者只需集中注意力解决处理过程就可以了。

使用Digester,需要完成以下几个基本步骤:

     *建立一个org.apache.commons.digester.Digester实例。这个实例可以安全地重用,不过必须满足1)完整地解析所有请求,2)这个实例不能用于多线程。
     *为解析器设置需要的Digester Configuration Properties,需要设置至少一个得Digester Configuration Properties(详细可看表一)。
     *把任何想初始化的对象压入Digester的对象堆栈里。
     *注册所有元素匹配模式,元素匹配模式将处理规则与XML元素联系起来。
     *调用digester.parse()方法,指定一个参数,参数为XML文件的完整名称。使用时必须抛出IOException或SAXException违例或者任何执行时可能抛出的任何违例。


Digester可以用作SAX事件处理器,按以下步骤操作:
    * 创建一个SAX解析器(可以使用JAXP APIs或者其他)
    * 为解析器设置需要的Digester Configuration Properties.
    * 创建一个org.apache.commons.digester.Digester实例.
    * 把任何想初始化的对象压入Digester的对象堆栈里.
    * 为Digester实例注册模式和规则.
    * 调用parser.parse(inputSource, digester)方法.


二、Digester Configuration Properties

PropertyDescription
classLoaderYou can optionally specify the class loader that will be used to load classes when required by the ObjectCreateRule and FactoryCreateRule rules. If not specified, application classes will be loaded from the thread's context class loader (if the useContextClassLoader property is set to true) or the same class loader that was used to load the Digester class itself.
errorHandlerYou can optionally specify a SAX ErrorHandler that is notified when parsing errors occur. By default, any parsing errors that are encountered are logged, but Digester will continue processing as well.
namespaceAwareA boolean that is set to true to perform parsing in a manner that is aware of XML namespaces. Among other things, this setting affects how elements are matched to processing rules. See Namespace Aware Parsing for more information.
xincludeAwareA boolean that is set to true to perform parsing in a manner that is aware of XInclude W3C specification. This setting is only effective if the parsing is already configured to be namespace aware.
ruleNamespaceURIThe public URI of the namespace for which all subsequently added rules are associated, or null for adding rules that are not associated with any namespace. See Namespace Aware Parsing for more information.
rulesThe Rules component that actually performs matching of Rule instances against the current element nesting pattern is pluggable. By default, Digester includes a Rules implementation that behaves as described in this document. See Pluggable Rules Processing for more information.
useContextClassLoaderA boolean that is set to true if you want application classes required by FactoryCreateRule and ObjectCreateRule to be loaded from the context class loader of the current thread. By default, classes will be loaded from the class loader that loaded this Digester class. NOTE - This property is ignored if you set a value for the classLoader property; that class loader will be used unconditionally.
validatingA boolean that is set to true if you wish to validate the XML document against a Document Type Definition (DTD) that is specified in its DOCTYPE declaration. The default value of false requests a parse that only detects "well formed" XML documents, rather than "valid" ones.

 

三、Digester 对象堆栈
    * clear() - 清除对象堆栈里的所有内容。
    * peek() - 以参数的方式返回对象堆栈顶部的对象,但不会清除它.
    * pop() - 清除对象堆栈顶部的对象,并且返回它.
    * push() - 压入一个新对象到对象堆栈的顶部。

 

四、元素匹配模式
元素匹配模式将处理规则与XML元素联系起来。

   <a>        -- Matches pattern "a"
    <b>       -- Matches pattern "a/b"
      <c/>    -- Matches pattern "a/b/c"
      <c/>    -- Matches pattern "a/b/c"
    </b>
    <b>       -- Matches pattern "a/b"
      <c/>    -- Matches pattern "a/b/c"
      <c/>    -- Matches pattern "a/b/c"
      <c/>    -- Matches pattern "a/b/c"
    </b>
  </a>

添加规则
  Rule ruleA = new ObjectCreateRule();
  Rule ruleB = new SetNextRule();
  Rule ruleC = new SetPropertiesRule();

  digester.addRule("*/a", ruleA);
  digester.addRule("*/a", ruleB);
  digester.addRule("x/a", ruleA);
  digester.addRule("x/a", ruleB);
  digester.addRule("x/a", ruleC);

 


五、简单例子

在上面的示例中,与'datasource/datasource'相关联的规则将会运行2次。


1)匹配模式

     <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>

2)XML文件
<?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>


3)调用
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");

 


六、下载
ajava.org下载地址:http://ajava.org/tool/xml/10215.html
官方下载地址:http://commons.apache.org/digester/download_digester.cgi

 

七、参考资料
主站地址:http://commons.apache.org/digester/
Digester 2.0 API文档地址:http://commons.apache.org/digester/commons-digester-2.0/docs/api/
Digester 2.0用户指南:http://commons.apache.org/digester/commons-digester-2.0/docs/api/org/apache/commons/digester/package-summary.html

 

八、结束语
本文对Digester作了简单的介绍,由于本人水平有限,如发现有错误纰漏,请指正。
联系方式:
网站:http://ajava.org
QQ:76662116
EM:chinesedocument@gamil.com

 

九、作者简介
mark,http://ajava.org站长,软件工程师,从事多年软件开发。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值