- 浏览: 166543 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
crabboy:
goodhttp://veerle.duoh.com/comm ...
表格效果 -
lunvsxiao:
好呀,省的自己记那么多了。。
java常用正则表达式
一、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
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站长,软件工程师,从事多年软件开发。
发表评论
-
取文件MD5的应用
2011-09-14 14:06 683import java.io.File; import ja ... -
用 Quartz 进行作业调度
2010-10-09 11:18 822Quartz 是个开源的作业调度框架,为在 Java 应用程序 ... -
Java读取中文文件出现乱码解决方法
2010-09-04 11:22 813前些天在读取一个UTF-8文件时,中文出现乱码,现在将处 ... -
Java项目中获取文件路径的问题总结
2010-09-03 11:36 0在项目中,或多或少都会涉及到获取文件的路径,然而处理也是多种多 ... -
java文件读取 编码方式设置
2010-09-02 16:28 877FileInputStream fr = new FileIn ... -
java获取项目路径
2010-08-25 12:00 2037在jsp和class文件中调用的相对路径不同。 在jsp里 ... -
websphere升级后,URLRewriter失效的处理办法
2010-05-24 13:20 1584进入控制台-》服务器-》应用程序服务器-》Web 容器 > ... -
模态窗口在web中的应用
2010-03-27 12:57 1576在web开发中,很多地方可能需要弹出模态窗口来进行处理,但在模 ... -
java文件上传本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/1
2010-03-05 18:38 1327Apache Commons fileUpload实现文件上传 ... -
Tomcat的class加载的优先顺序一览
2010-02-21 22:16 1308Tomcat的class加载的优先顺序一览1.最先是$JAVA ... -
删除eclipse或者MyEclipse记录的workspace
2010-01-13 10:28 1531在开发过程中可能建立了很多工作台,但随着开发的进行,可能有的工 ... -
文件上传组件的编写
2009-12-18 10:45 1207张孝祥的教程:文件的上传组件编写,希望对你有帮助! -
JAVA对象比较器Comparator
2009-10-24 17:45 1791JAVA对象比较器Comparator ... -
JAVA中通过类名,自动创建对象,并且合理初始化(自动找到合适的构造方法)
2009-10-24 16:03 2575JAVA中通过类名,自动创建对象,并且合理初始化(自动找到合适 ... -
Java软件架构师所要学的东西
2009-09-23 15:05 1470选择的范围太广,可以读的书太多,往往容易无所适从。我想就我自己 ... -
Java正则表达式
2009-09-14 15:01 1225众所周知,在程序开发中,难免会遇到需要匹配、查找、替换、判断字 ... -
httpclient4相关资料记录
2009-09-10 22:25 1547import java.net.Socket;import o ... -
用commons的HttpClient和FileUpload写的文件上传下载类
2009-09-09 17:12 2141用commons的HttpClient和FileUpload写 ... -
use HTTPClient for loading image from URL
2009-09-09 15:01 1643读取JPEG,PNG,TIFF图像格式的文件。 ... -
Quartz CronTrigger最完整配置说明
2009-09-08 09:30 1179CronTrigger配置格式:格式: [秒] [分] [小时 ...
相关推荐
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是什么,它是用来解析xml文件的的工具,是jakarta开源项目下commons的一个子项目,它能让程序员更方便的解析xml文件,而不需要了解底层的工作细节。 如果要使用Digester作为xml文件的解析,请到jakarta...
首先Digester是什么,它是用来解析xml文件的的工具,是jakarta开源项目下commons的一个子项目,它能让程序员更方便的解析xml文件,而不需要了解底层的工作细节。 Digester 解析xml文件 实例。
中文-英文对照文档,中英对照文档,java,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压 【***.jar中文文档.zip】,再解压其中的 【***-...
中文-英文对照文档,中英对照文档,java,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压 【***.jar中文文档.zip】,再解压其中的 【***-...
中文-英文对照文档,中英对照文档,java,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压 【***.jar中文文档.zip】,再解压其中的 【***-...
Sitemesh 是一个基于WEB页面的布局、装饰以及应用整合的开源框架。它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的 banner,一致的版权,等等。它不仅仅能处理动态的内容,...
Xanot是XOM(从Xml到Object Mapper)。 与众所周知的Apache Digester非常相似。 但是它使用了Java 5注释功能。 这样,这些类可以“告诉”解析器如何将xml数据映射到其对象模型中。
来自《深入剖析Tomcat》 ...前导工作: org.apache.catalina....析server.xml文件,获取所有对象 StandardServer的初始化(),start()方法调用所有的服务组件(数组)StandardService的初始化(),start()方法,
Java SAP Bapi执行框架。 该框架将通过从XML文件读取SAP描述符来执行SAP R3功能。 需要Java Connector,Commons-Digester提供数据类型转换,聚合函数,本地化等更多信息。