亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

開源XML處理包:Digester

系統 2484 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條評論
主站蜘蛛池模板: 成人午夜私人影院入口 | 一级女毛片 | 日韩在线第三页 | 久久久久久久久中文字幕 | 亚洲va欧美va国产va天堂影 | 国产毛片一区二区三区精品 | 婷婷四房综合激情五月在线 | 日韩午夜免费视频 | 加勒比黑人在线 | 久热re国产手机在线观看 | 亚洲成人视 | 99精品国产高清一区二区 | 亚洲色图国产精品 | 亚洲视频www | 久久国产精品只做精品 | 天天天做天天天天爱天天想 | 天天做天天玩天天爽天天 | 久久免费资源 | 伊人久久91 | 久久亚洲精品永久网站 | 久草最新网址 | 朴妮唛禁福利视频在线 | 青青青国产免费线在 | 国产五月婷婷 | 国产精品福利在线观看入口 | 日本大片免a费观看在线 | 国产综合欧美日韩视频一区 | 青草视频国产 | 日韩精品国产自在久久现线拍 | 欧美日韩久久 | 青草青草久热精品视频在线观看 | 国产精品一二区 | 久视频免费精品6 | 国产精品视频免费看 | 狼人伊人干 | 骚婷婷| 91精品在线免费 | 大伊香蕉在线精品不卡视频 | 夜夜狠狠狠狠 | 99热这里只有精品在线播放 | 奇米色7777|