开源XML处理包:Digester

系统 1842 0

 

 

 

 

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

Property Description
classLoader You 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.
errorHandler You 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.
namespaceAware A 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.
xincludeAware A 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.
ruleNamespaceURI The 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.
rules The 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.
useContextClassLoader A 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.
validating A 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 站长,软件工程师,从事多年软件开发。

开源XML处理包:Digester


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论