?
?
?
?
一、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
站長,軟件工程師,從事多年軟件開發。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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