?
?
?
?
一、Digester簡(jiǎn)介
Jakarta Commons Digester是Apache小組的Jakarta項(xiàng)目下的子項(xiàng)目,是目前比較流行的、開源的XML文件處理包。目前最新版本是2.0版本。
許多應(yīng)用都需要處理XML格式的數(shù)據(jù),這時(shí)Digester是個(gè)很好的選擇。Digeste提供事件驅(qū)動(dòng)管理器處理XML文件。開發(fā)者可以使用熟悉簡(jiǎn)單的API,以SAX方式解析XML。提供開發(fā)友好的SAX事件接口,使開發(fā)者只需集中注意力解決處理過程就可以了。
使用Digester,需要完成以下幾個(gè)基本步驟:
???? *建立一個(gè)org.apache.commons.digester.Digester實(shí)例。這個(gè)實(shí)例可以安全地重用,不過必須滿足1)完整地解析所有請(qǐng)求,2)這個(gè)實(shí)例不能用于多線程。
???? *為解析器設(shè)置需要的Digester Configuration Properties,需要設(shè)置至少一個(gè)得Digester Configuration Properties(詳細(xì)可看表一)。
???? *把任何想初始化的對(duì)象壓入Digester的對(duì)象堆棧里。
???? *注冊(cè)所有元素匹配模式,元素匹配模式將處理規(guī)則與XML元素聯(lián)系起來。
???? *調(diào)用digester.parse()方法,指定一個(gè)參數(shù),參數(shù)為XML文件的完整名稱。使用時(shí)必須拋出IOException或SAXException違例或者任何執(zhí)行時(shí)可能拋出的任何違例。
Digester可以用作SAX事件處理器,按以下步驟操作:
??? * 創(chuàng)建一個(gè)SAX解析器(可以使用JAXP APIs或者其他)
??? * 為解析器設(shè)置需要的Digester Configuration Properties.
??? * 創(chuàng)建一個(gè)org.apache.commons.digester.Digester實(shí)例.
??? * 把任何想初始化的對(duì)象壓入Digester的對(duì)象堆棧里.
??? * 為Digester實(shí)例注冊(cè)模式和規(guī)則.
??? * 調(diào)用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 對(duì)象堆棧
??? * clear() - 清除對(duì)象堆棧里的所有內(nèi)容。
??? * peek() - 以參數(shù)的方式返回對(duì)象堆棧頂部的對(duì)象,但不會(huì)清除它.
??? * pop() - 清除對(duì)象堆棧頂部的對(duì)象,并且返回它.
??? * push() - 壓入一個(gè)新對(duì)象到對(duì)象堆棧的頂部。
?
四、元素匹配模式
元素匹配模式將處理規(guī)則與XML元素聯(lián)系起來。
?? <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>
添加規(guī)則
? 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);
?
五、簡(jiǎn)單例子
在上面的示例中,與'datasource/datasource'相關(guān)聯(lián)的規(guī)則將會(huì)運(yùn)行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)調(diào)用
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
?
八、結(jié)束語(yǔ)
本文對(duì)Digester作了簡(jiǎn)單的介紹,由于本人水平有限,如發(fā)現(xiàn)有錯(cuò)誤紕漏,請(qǐng)指正。
聯(lián)系方式:
網(wǎng)站:
http://ajava.org
QQ:76662116
EM:
chinesedocument@gamil.com
?
九、作者簡(jiǎn)介
mark,
http://ajava.org
站長(zhǎng),軟件工程師,從事多年軟件開發(fā)。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
