`
huanyq2008
  • 浏览: 166543 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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

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站长,软件工程师,从事多年软件开发。

分享到:
评论

相关推荐

    Java开发常用jar包

    2.commons-digester.jar:方便地将XML文档所定义的元素转化为JAVA对象。 3.commons-lang.jar; 4.commons-collection.jar包: 5.commons-io.jar包:FileUtil.readLine(),就在这个jar包。 6.commons-HttpClient.jar包...

    Digester笔记

    首先Digester是什么,它是用来解析xml文件的的工具,是jakarta开源项目下commons的一个子项目,它能让程序员更方便的解析xml文件,而不需要了解底层的工作细节。 如果要使用Digester作为xml文件的解析,请到jakarta...

    Digester 实例

    首先Digester是什么,它是用来解析xml文件的的工具,是jakarta开源项目下commons的一个子项目,它能让程序员更方便的解析xml文件,而不需要了解底层的工作细节。 Digester 解析xml文件 实例。

    commons-digester-2.1.jar中文-英文对照文档.zip

    中文-英文对照文档,中英对照文档,java,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压 【***.jar中文文档.zip】,再解压其中的 【***-...

    commons-digester-1.8.1.jar中文-英文对照文档.zip

    中文-英文对照文档,中英对照文档,java,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压 【***.jar中文文档.zip】,再解压其中的 【***-...

    commons-digester3-3.2.jar中文-英文对照文档.zip

    中文-英文对照文档,中英对照文档,java,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压 【***.jar中文文档.zip】,再解压其中的 【***-...

    java开发常用jar包

    Sitemesh 是一个基于WEB页面的布局、装饰以及应用整合的开源框架。它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的 banner,一致的版权,等等。它不仅仅能处理动态的内容,...

    Xanot an Xml to Object Mapper-开源

    Xanot是XOM(从Xml到Object Mapper)。 与众所周知的Apache Digester非常相似。 但是它使用了Java 5注释功能。 这样,这些类可以“告诉”解析器如何将xml数据映射到其对象模型中。

    myTomcat:WebServer + Tomcat源码分析总结

    来自《深入剖析Tomcat》 ...前导工作: org.apache.catalina....析server.xml文件,获取所有对象 StandardServer的初始化(),start()方法调用所有的服务组件(数组)StandardService的初始化(),start()方法,

    Jabapper - Java SAP Bapi Executor-开源

    Java SAP Bapi执行框架。 该框架将通过从XML文件读取SAP描述符来执行SAP R3功能。 需要Java Connector,Commons-Digester提供数据类型转换,聚合函数,本地化等更多信息。

Global site tag (gtag.js) - Google Analytics