<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    每日一得

    不求多得,只求一得 about java,hibernate,spring,design,database,Ror,ruby,快速開發(fā)
    最近關(guān)心的內(nèi)容:SSH,seam,flex,敏捷,TDD
    本站的官方站點(diǎn)是:顛覆軟件

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      220 隨筆 :: 9 文章 :: 421 評(píng)論 :: 0 Trackbacks
    key words: Digester? 解析xml

    假設(shè)有下列xml文件:
    <?xml?version='1.0'?encoding='utf-8'?>
    <address-book>
    ????
    <contact?myType="individual">
    ????????
    <name>Zane?Pasolini</name>
    ????????
    <address>999?W.?Prince?St.</address>
    ????????
    <city>New?York</city>
    ????????
    <province>NY</province>
    ????????
    <postalcode>10013</postalcode>
    ????????
    <country>USA</country>
    ????????
    <telephone>1-212-345-6789</telephone>
    ????
    </contact>
    ????
    <contact?myType="business">
    ????????
    <name>SAMOFIX?d.o.o.</name>
    ????????
    <address>Ilica?47-2</address>
    ????????
    <city>Zagreb</city>
    ????????
    <province></province>
    ????????
    <postalcode>10000</postalcode>
    ????????
    <country?from="cn">Croatia</country>
    ????????
    <telephone>385-1-123-4567</telephone>
    ????
    </contact>
    </address-book>

    這是一份常用到的文件,現(xiàn)在我們需要將之映射到j(luò)ava bean,用Digester解析顯得非常簡(jiǎn)單
    public?class?AddressBookParser
    {
    ????
    /**
    ?????*?Prints?the?contact?information?to?standard?output.
    ?????*
    ?????*?
    @param?contact?the?<code>Contact</code>?to?print?out
    ?????
    */
    ????
    public?void?addContact(Contact?contact)
    ????{
    ????????System.out.println(
    "TYPE:?"?+?contact.getType());
    ????????System.out.println(
    "NAME:?"?+?contact.getName());
    ????????System.out.println(
    "????ADDRESS:????"?+?contact.getAddress());
    ????????System.out.println(
    "????CITY:???????"?+?contact.getCity());
    ????????System.out.println(
    "????PROVINCE:???"?+?contact.getProvince());
    ????????System.out.println(
    "????POSTALCODE:?"?+?contact.getPostalcode());
    ????????System.out.println(
    "????COUNTRY:????"?+?contact.getCountry());
    ????????System.out.println(
    "????COUNTRY-From:????"?+?contact.getCountryFrom());
    ????????System.out.println(
    "????TELEPHONE:??"?+?contact.getTelephone());
    ????}

    ????
    /**
    ?????*?Configures?Digester?rules?and?actions,?parses?the?XML?file?specified
    ?????*?as?the?first?argument.
    ?????*
    ?????*?
    @param?args?command?line?arguments
    ?????
    */
    ????
    public?static?void?main(String[]?args)?throws?IOException,?SAXException
    ????{
    ????????
    //?instantiate?Digester?and?disable?XML?validation
    ????????Digester?digester?=?new?Digester();
    ????????digester.setValidating(
    false);

    ????????
    //?instantiate?AddressBookParser?class
    ????????digester.addObjectCreate("address-book",?AddressBookParser.class?);
    ????????
    //?instantiate?Contact?class
    ????????digester.addObjectCreate("address-book/contact",?Contact.class?);

    ????????
    //?set?type?property?of?Contact?instance?when?'type'?attribute?is?found
    ????????
    //對(duì)有屬性的值通過setProperties方法

    ????????digester.addSetProperties(
    "address-book/contact",?????????"myType",?"type"?);

    ????????
    //?set?different?properties?of?Contact?instance?using?specified?methods
    ????????
    //addCallMethod與addBeanPropertySetter等價(jià)
    ????????
    //?參數(shù)?0代表一個(gè)參數(shù),默認(rèn)就是當(dāng)前讀的數(shù)據(jù)

    ????????digester.addCallMethod(
    "address-book/contact/name",???????"setName",?0);
    ????????digester.addCallMethod(
    "address-book/contact/address",????"setAddress",?0);
    ????????digester.addCallMethod(
    "address-book/contact/address",????"setAddress",0);
    ????????digester.addCallMethod(
    "address-book/contact/city",???????"setCity",?0);
    ????????digester.addCallMethod(
    "address-book/contact/province",???"setProvince",?0);
    ????????digester.addCallMethod(
    "address-book/contact/postalcode",?"setPostalcode",?0);
    ????????digester.addCallMethod(
    "address-book/contact/country",????"setCountry",?0);



    ????????
    //增加country的屬性?:?from
    ????????digester.addSetProperties("address-book/contact/country","from","countryFrom");
    ????????digester.addCallMethod(
    "address-book/contact/telephone",??"setTelephone",?0);

    ????????
    //?call?'addContact'?method?when?the?next?'address-book/contact'?pattern?is?seen
    ????????digester.addSetNext("address-book/contact",???????????????"addContact"?);

    ????????
    //?now?that?rules?and?actions?are?configured,?start?the?parsing?process
    ????????AddressBookParser?abp?=?(AddressBookParser)?digester.parse(new?File("c:\\addressbook.xml"));
    ????}

    ????
    /**
    ?????*?JavaBean?class?that?holds?properties?of?each?Contact?entry.
    ?????*?It?is?important?that?this?class?be?public?and?static,?in?order?for
    ?????*?Digester?to?be?able?to?instantiate?it.
    ?????
    */
    ????
    public?static?class?Contact
    ????{
    ????????
    private?String?type;
    ????????
    private?String?name;
    ????????
    private?String?address;
    ????????
    private?String?city;
    ????????
    private?String?province;
    ????????
    private?String?postalcode;
    ????????
    private?String?country;
    ??????? //增加一個(gè)country的屬性:?from
    ????????private?String?countryFrom;
    ????????private?String?telephone;

    ????????
    public?void?setType(String?newType)
    ????????{
    ????????????type?
    =?newType;
    ????????}
    ????????
    public?String?getType()
    ????????{
    ????????????
    return?type;
    ????????}

    ????????
    public?void?setName(String?newName)
    ????????{
    ????????????name?
    =?newName;
    ????????}
    ????????
    public?String?getName()
    ????????{
    ????????????
    return?name;
    ????????}

    ????????
    public?void?setAddress(String?newAddress)
    ????????{
    ????????????address?
    =?newAddress;
    ????????}
    ????????
    public?String?getAddress()
    ????????{
    ????????????
    return?address;
    ????????}

    ????????
    public?void?setCity(String?newCity)
    ????????{
    ????????????city?
    =?newCity;
    ????????}
    ????????
    public?String?getCity()
    ????????{
    ????????????
    return?city;
    ????????}

    ????????
    public?void?setProvince(String?newProvince)
    ????????{
    ????????????province?
    =?newProvince;
    ????????}
    ????????
    public?String?getProvince()
    ????????{
    ????????????
    return?province;
    ????????}

    ????????
    public?void?setPostalcode(String?newPostalcode)
    ????????{
    ????????????postalcode?
    =?newPostalcode;
    ????????}
    ????????
    public?String?getPostalcode()
    ????????{
    ????????????
    return?postalcode;
    ????????}

    ????????
    public?void?setCountry(String?newCountry)
    ????????{
    ????????????country?
    =?newCountry;
    ????????}
    ????????
    public?String?getCountry()
    ????????{
    ????????????
    return?country;
    ????????}

    ????????
    public?void?setTelephone(String?newTelephone)
    ????????{
    ????????????telephone?
    =?newTelephone;
    ????????}
    ????????
    public?String?getTelephone()
    ????????{
    ????????????
    return?telephone;
    ????????}

    ????????
    public?String?getCountryFrom()?{
    ????????????
    return?countryFrom;
    ????????}

    ????????
    public?void?setCountryFrom(String?countryFrom)?{
    ????????????
    this.countryFrom?=?countryFrom;
    ????????}
    ????}
    }


    AjaxChat 中的讀取房間信息的方式顯得更簡(jiǎn)潔
    房間的xml配置文件如下:
    <rooms>
    ??
    <room?id="1"?name="General?Topics"?/>
    ??
    <room?id="2"?name="Programming"?/>
    ??
    <room?id="3"?name="Movies"?/>
    ??
    <room?id="4"?name="Music"?/>
    ??
    <room?id="5"?name="Television"?/>
    </rooms>

    解析代碼如下 :
    public?synchronized?void?init(InputStream?isConfigFile)?{

    ????????log.debug(
    "init()");
    ????????
    if?(isConfigFile?!=?null)?{
    ????????????
    //?Read?in?rooms?config?and?create?beans,?hand?off?to?DAO.
    ????????????Digester?digester?=?new?Digester();
    ????????????digester.setValidating(
    false);
    ????????????digester.push(
    this);
    ????????????digester.addObjectCreate(
    "rooms/room",
    ????????????????????
    "org.apache.struts.apps.ajaxchat.dto.RoomDTO");
    ??? ?? ?? ? //注意這里,如果xl的屬性名稱和bean的屬性名稱完全對(duì)應(yīng),則直接提供xml的位置即可
    ????????????digester.addSetProperties(
    "rooms/room");
    ????????????digester.addSetNext(
    "rooms/room",?"addRoom");
    ????????????
    try?{
    ????????????????digester.parse(isConfigFile);
    ????????????????log.info(
    "*****?Rooms?=?"?+?rooms);
    ????????????}?
    catch?(IOException?ioe)?{
    ????????????????ioe.printStackTrace();
    ????????????}?
    catch?(SAXException?se)?{
    ????????????????se.printStackTrace();
    ????????????}
    ????????}

    ????}?
    //?End?init().

    如果在xml文件中增加非attribute則更改后的配置文件如下:

    <rooms>
    ??
    <room?id="1"?name="General?Topics"?/>
    ??
    <room?id="2"?name="Programming"?/>
    ??
    <room?id="3"?name="Movies"?/>
    ??
    <room?id="4"?name="Music"?/>
    ??
    <room?id="5"?name="Television"?/>
    ??
    <room>
    ????
    <id>6</id>
    ????
    <name>shit</name>
    ??
    </room>
    ??
    <room>
    ????
    <id>7</id>
    ????
    <name>haha</name>
    ??
    </room>
    </rooms>
    對(duì)應(yīng)的解析如下:
    public?synchronized?void?init(InputStream?isConfigFile)?{

    ????????log.debug(
    "init()");
    ????????
    if?(isConfigFile?!=?null)?{
    ????????????
    //?Read?in?rooms?config?and?create?beans,?hand?off?to?DAO.
    ????????????Digester?digester?=?new?Digester();
    ????????????digester.setValidating(
    false);
    ????????????digester.push(
    this);
    ????????????digester.addObjectCreate(
    "rooms/room",
    ????????????????????
    "org.apache.struts.apps.ajaxchat.dto.RoomDTO");
    ????????????digester.addSetProperties(
    "rooms/room");
    ??? ?? ?? ? //增加addCallMethod方法
    ????????????digester.addCallMethod(
    "rooms/room/id","setId",0);
    ????????????digester.addCallMethod(
    "rooms/room/name","setName",0);
    ????????????digester.addSetNext(
    "rooms/room",?"addRoom");
    ????????????
    try?{
    ????????????????digester.parse(isConfigFile);
    ????????????????log.info(
    "*****?Rooms?=?"?+?rooms);
    ????????????}?
    catch?(IOException?ioe)?{
    ????????????????ioe.printStackTrace();
    ????????????}?
    catch?(SAXException?se)?{
    ????????????????se.printStackTrace();
    ????????????}
    ????????}

    ????}?
    //?End?init().

    posted on 2006-09-06 23:32 Alex 閱讀(20871) 評(píng)論(19)  編輯  收藏 所屬分類: java

    評(píng)論

    # re: 用Digester解析xml到bean 2006-09-07 16:01 山風(fēng)小子
    寫的很好:-)  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2006-09-08 22:10 巴哈姆特
    不錯(cuò),,值得參考!  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2006-10-19 17:33 villagehead
    不錯(cuò)。

    另外,這兩句:
    digester.addCallMethod("rooms/room/id","setId",0);
    digester.addCallMethod("rooms/room/name","setName",0);

    還可以用
    digester.addBeanPropertySetter("rooms/room/id");
    digester.addBeanPropertySetter("rooms/room/name");

    的形式

    另外的另外,回字有4種寫法。;)  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2007-01-26 18:00 zhyiwww
    看過,有收獲。  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2007-02-12 09:19 qq
    不錯(cuò),非常不錯(cuò)!  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2007-02-12 10:05 qq
    我自己實(shí)踐了一下,覺得效果非常不錯(cuò)!省去了DOM操作的細(xì)節(jié),但是缺點(diǎn)是操作文檔不靈活。需要寫很多代碼。  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2007-10-25 12:54 wdd
    為什么按你一模一樣的敲上!并且是運(yùn)行是報(bào)此錯(cuò)呢?
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.apache.commons.digester.Digester.<init>(Digester.java:299)
    at AddressBookParser.main(AddressBookParser.java:34)  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2008-01-24 09:05 JAVA 小白
    <persistence-unit name="aa">
    <class>a1</class>
    <class>a2</class>
    <class>a3</class>
    </persistence-unit>

    <class>標(biāo)簽怎么實(shí)現(xiàn)

    請(qǐng)指教。  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2008-05-11 17:42 學(xué)海無涯
    為什么javabean必須是靜態(tài)的類?
    請(qǐng)高手指點(diǎn),謝謝了  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2008-07-24 14:07 aga
    javabean不必一定是靜態(tài)類
    to ../../../*
    你沒有加包
    commons-digester\commons-logging\commons-beanutil似乎還有一個(gè)
      回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean[未登錄] 2008-08-30 23:46 54powerman
    手動(dòng)寫bean,夠麻煩  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2009-06-01 16:18 sheng
    如果層次太深 還是讓人很傷心的 它有沒有提供 //province 這中形式呢

    它是xpath的檢索方式嗎??

    剛學(xué)習(xí) 多指教

    謝謝  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2009-07-02 16:45 ×××××
    @wdd
    少包沒導(dǎo)入  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2009-11-24 09:39 路過
    當(dāng)屬性為int時(shí),怎么調(diào)用addCallMethod方法?  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2010-10-23 21:11 GANK
    寫的不錯(cuò),值得學(xué)習(xí)?。?nbsp; 回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2014-04-16 14:03 ferrari
    包導(dǎo)錯(cuò)了
    這個(gè)目錄下的import org.apache.commons.digester.Digester;  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2014-07-01 14:17 fsgs
    gdsfssdgdsg  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean 2014-07-01 14:17 fsgs
    不是太會(huì)
    @  回復(fù)  更多評(píng)論
      

    # re: 用Digester解析xml到bean[未登錄] 2014-08-07 17:16 lee
    哪四種@villagehead
      回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 成年人网站免费视频| 最近免费中文字幕MV在线视频3 | 亚洲一区动漫卡通在线播放| 国产免费AV片在线观看| 一本到卡二卡三卡免费高| 免费一级国产生活片| 欧洲精品码一区二区三区免费看| 可以免费观看一级毛片黄a| 男女超爽视频免费播放| 亚洲男人第一无码aⅴ网站 | 福利免费在线观看| 亚洲精品无码久久一线| 久久午夜免费鲁丝片| 亚洲综合久久1区2区3区| 成人免费激情视频| 中国一级毛片视频免费看| 亚洲精品国产品国语在线| 久久99青青精品免费观看| 日本免费一区尤物| 色多多免费视频观看区一区| 国产亚洲精品精品国产亚洲综合| 国产午夜成人免费看片无遮挡| 亚洲三级电影网址| 免费无码又黄又爽又刺激| 亚洲综合一区二区精品久久| 成人爽A毛片免费看| 青青视频免费在线| 亚洲AV永久无码精品水牛影视| 免费又黄又爽又猛大片午夜| 国产亚洲综合久久系列| 麻豆高清免费国产一区| 鲁死你资源站亚洲av| 亚洲午夜久久久久妓女影院| 国内永久免费crm系统z在线| 亚洲免费一级视频| 免费大片在线观看网站| 免费网站看av片| 亚洲国产精品无码久久| 亚洲人成人一区二区三区| 男女免费观看在线爽爽爽视频 | 亚洲精品国产肉丝袜久久|