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

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

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


    隨筆-35  評論-97  文章-0  trackbacks-0

    今天看了八進制的《利用DigesterXML轉換為Java對象》,感覺挺好。你可以在這里http://www.cnblogs.com/bjzhanghao/archive/2007/05/15/125747.html訪問。加上混北民工的在評論中更詳細一點的例子。終于對Digester有了初步的了解。但是,對于詳細返回對象,在八進制和混北民工的例子中表現(xiàn)得不是十分明朗。下面,我對例子做了一些修改,使Digester的規(guī)則表現(xiàn)清晰一些,另外添加配置規(guī)則來代替代碼入注規(guī)則的運用方法。

     

    注冊表ChartRegistry:

    public class ChartRegistry
    {
        List
    <ChartConfig> registry = new ArrayList<ChartConfig>();
        
    public void addChart(ChartConfig obj)
        
    {
            registry.add(obj);
        }

        
        
    public java.util.List<ChartConfig> getRegistry()
        
    {
            
    return registry;
        }


        
    public String toString() {
            
    return ReflectionToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
        }

    }

     

    圖表設定器ChartConfig:

    public class ChartConfig implements Comparable
    {
        
    private String id;
        
    private boolean legendVisible;
        
    private String type;
        
    private int width;
        
    private String description;

        
    public String getDescription()
        
    {
            
    return description;
        }

        
    public void setDescription(String description)
        
    {
            
    this.description = description;
        }

        
    public String getId()
        
    {
            
    return id;
        }

        
    public void setId(String id)
        
    {
            
    this.id = id;
        }

        
    public boolean isLegendVisible()
        
    {
            
    return legendVisible;
        }

        
    public void setLegendVisible(boolean legendVisible)
        
    {
            
    this.legendVisible = legendVisible;
        }

        
    public String getType()
        
    {
            
    return type;
        }

        
    public void setType(String type)
        
    {
            
    this.type = type;
        }

        
    public int getWidth()
        
    {
            
    return width;
        }

        
    public void setWidth(int width)
        
    {
            
    this.width = width;
        }

        
    public String toString() {
            
    return ReflectionToStringBuilder.reflectionToString(this,ToStringStyle.MULTI_LINE_STYLE);
        }

        
    public int compareTo(Object o)
        
    {
            
    if(o instanceof ChartConfig){
                ChartConfig o1 
    = (ChartConfig)o;
                
    return this.id.compareTo(o1.id);
            }

            
    return -1;
        }

    }

     

    圖表數據設定(簡化版)charts.xml:

    <?xml version="1.0" encoding="GBK" ?>
    <charts>
        
    <chart id="BarChart1">
            
    <legendVisible>false</legendVisible>
            
    <type>中國</type>
            
    <width>500</width>
            
    <description>11</description>
        
    </chart>
        
    <chart id="BarChart2">
            
    <legendVisible>true</legendVisible>
            
    <type>美國</type>
            
    <width>500</width>
            
    <description>22</description>
        
    </chart>
    </charts>

     

    配置規(guī)則設定rule.xml:

    <?xml version='1.0'?>
    <!DOCTYPE digester-rules 
      PUBLIC "-//Jakarta Apache //DTD digester-rules XML V1.0//EN" 
        "file:///../digester/dtds/digester-rules.dtd"
    >

    <digester-rules>
        
    <pattern value="charts">  <!-- 遇上charts節(jié)點時 -->
            
    <object-create-rule classname="study.jakarta.commons.digester.ChartRegistry" />  <!-- 創(chuàng)建對象 -->
            
    <set-properties-rule /> <!-- 設置屬性 -->
            
    <pattern value="chart"> <!-- 遇上charts節(jié)點時 -->
                
    <object-create-rule classname="study.jakarta.commons.digester.ChartConfig" /> <!-- 創(chuàng)建對象 -->
                
    <set-next-rule methodname="addChart" paramtype="study.jakarta.commons.digester.ChartConfig"/> <!-- 調用定對象方法 -->
                
    <set-properties-rule /> <!-- 設置屬性 -->
                
    <bean-property-setter-rule pattern="legendVisible" /> <!-- 調用對象的setter方法 -->
                
    <bean-property-setter-rule pattern="type" />
                
    <bean-property-setter-rule pattern="width" />
                
    <bean-property-setter-rule pattern="description" />
            
    </pattern>
        
    </pattern>
    </digester-rules>

     

    跑一下試試看:

    public class DigesterTest
    {
        
    public static void main(String[] args)
        
    {
            
    try
            
    {
                String charts 
    = "F:\\計算機\\jakarta-commons\\jakarta-commons-cookbook-code\\examples\\study\\jakarta\\commons\\digester\\charts.xml";
                String rule
    ="F:\\計算機\\jakarta-commons\\jakarta-commons-cookbook-code\\examples\\study\\jakarta\\commons\\digester\\rules.xml";
                DigesterTest test
    = new DigesterTest();
                System.out.println(
    "======================= getChartRegistryWithCodeRule ==========================");
                ChartRegistry c
    = test.getChartRegistryWithCodeRule(new File(charts));
                System.out.println(c);
                System.out.println(
    "======================= getChartRegistryWithCodeRule ==========================");
                System.out.println(test.getChartRegistryWithFileRule(
    new File(charts),new File(rule)));
            }

            
    catch(Exception e)
            
    {
                e.printStackTrace();
            }

        }

        
        
    //代碼中設定規(guī)則
        private ChartRegistry getChartRegistryWithCodeRule(File charts) throws IOException, SAXException{
            
                    InputStream is 
    = new FileInputStream(charts);
                    Digester digester 
    = new Digester();
                    digester.setValidating(
    false);//不進行XML與相應的DTD的合法性驗證
                    
                    digester.push(
    new ChartRegistry());                         //創(chuàng)建一個ChartRegistry對象, 你這樣用這種形式建立頂對象(選用這種方式,注釋下一行)
                    
    //digester.addObjectCreate("charts", ChartRegistry.class);  //創(chuàng)建一個ChartRegistry對象, 你也這樣用這種形式建立頂對象(選用這種方式,注釋上一行)
                    digester.addSetProperties("charts");
                    digester.addObjectCreate(
    "charts/chart", ChartConfig.class); //當遇到<chart>時創(chuàng)建一個ChartConfig對象
                    digester.addSetProperties("charts/chart");  //根據<chart>元素的屬性(attribute),對剛創(chuàng)建的ChartConfig對象的屬性(property)進行設置
                    digester.addBeanPropertySetter("charts/chart/legendVisible");//當遇到<chart>的子元素<legendVisible>時將子元素值設置為ChartConfig對象的legendVisible屬性的setter方法
                    digester.addBeanPropertySetter("charts/chart/type");        //參考上面注釋
                    digester.addBeanPropertySetter("charts/chart/width");        //參考上面注釋
                    digester.addBeanPropertySetter("charts/chart/description"); //參考上面注釋
                    digester.addSetNext("charts/chart""addChart");        //當再次遇到<charts>的子元素<chart>時調用頂對象的addChart方法。
                    ChartRegistry c = (ChartRegistry)digester.parse(is);  //分析結束后,返回根元素
                    Collections.sort(c.getRegistry());
                    
    return c;
        }

        
        
    //配置中設定規(guī)則
        private ChartRegistry getChartRegistryWithFileRule(File charts,File rule) throws IOException, SAXException{
                Digester digester 
    = DigesterLoader.createDigester(rule.toURL());
                ChartRegistry c 
    = (ChartRegistry)digester.parse(charts);
                Collections.sort(c.getRegistry());
                
    return c;
        }

    }

     

    結果輸出:

    ======================= getChartRegistryWithCodeRule ==========================
    study.jakarta.commons.digester.ChartRegistry@16df84b[
      registry
    =[study.jakarta.commons.digester.ChartConfig@1aa57fb[
      id
    =BarChart1
      legendVisible
    =false
      type
    =中國
      width
    =500
      description
    =11
    ], study.jakarta.commons.digester.ChartConfig@763f5d[
      id
    =BarChart2
      legendVisible
    =true
      type
    =美國
      width
    =500
      description
    =22
    ]]
    ]
    ======================= getChartRegistryWithCodeRule ==========================
    study.jakarta.commons.digester.ChartRegistry@18f6235[
      registry
    =[study.jakarta.commons.digester.ChartConfig@78a212[
      id
    =BarChart1
      legendVisible
    =false
      type
    =中國
      width
    =500
      description
    =11
    ], study.jakarta.commons.digester.ChartConfig@1d85f79[
      id
    =BarChart2
      legendVisible
    =true
      type
    =美國
      width
    =500
      description
    =22
    ]]
    ]

     

    剛學,未免存在不當的地方,歡迎指正,交流。

    posted on 2007-05-23 15:00 三告習習 閱讀(5283) 評論(2)  編輯  收藏 所屬分類: Jakarta

    評論:
    # re: [jakarta-commons] 使用Digester解釋xml獲取java對象(代碼入注規(guī)則 或 配置規(guī)則 任你選!) 2009-04-29 15:22 | 小蟲旺福
    寫的很好。  回復  更多評論
      
    # re: [jakarta-commons] 使用Digester解釋xml獲取java對象(代碼入注規(guī)則 或 配置規(guī)則 任你選!) 2009-11-09 10:27 | Java小子
    樓主,運行起來怎么有錯啊?

    2009-11-9 10:18:24 org.apache.commons.digester.Digester startElement
    嚴重: Begin event threw exception
    java.lang.ClassNotFoundException: study.jakarta.commons.digester.ChartRegistry
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at org.apache.commons.digester.ObjectCreateRule.begin(ObjectCreateRule.java:205)
    at org.apache.commons.digester.Rule.begin(Rule.java:175)
    at org.apache.commons.digester.Digester.startElement(Digester.java:1563)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:533)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:798)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:878)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDispatcher.scanRootElementHook(XMLDocumentScannerImpl.java:1157)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1794)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
    at org.apache.commons.digester.Digester.parse(Digester.java:1842)
    at com.digester.beans.DigesterTest.getChartRegistryWithFileRule(DigesterTest.java:62)
    at com.digester.beans.DigesterTest.main(DigesterTest.java:26)
    java.lang.ClassNotFoundException: study.jakarta.commons.digester.ChartRegistry
    at org.apache.commons.digester.Digester.createSAXException(Digester.java:3333)
    at org.apache.commons.digester.Digester.createSAXException(Digester.java:3359)
    at org.apache.commons.digester.Digester.startElement(Digester.java:1566)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:533)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:798)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:878)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDispatcher.scanRootElementHook(XMLDocumentScannerImpl.java:1157)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1794)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
    at org.apache.commons.digester.Digester.parse(Digester.java:1842)
    at com.digester.beans.DigesterTest.getChartRegistryWithFileRule(DigesterTest.java:62)
    at com.digester.beans.DigesterTest.main(DigesterTest.java:26)  回復  更多評論
      

    只有注冊用戶登錄后才能發(fā)表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲视频免费观看| 国产成人AV免费观看| 午夜免费1000部| 久久久久亚洲AV无码永不| 青柠影视在线观看免费| 亚洲va久久久噜噜噜久久天堂| 免费无码一区二区三区蜜桃| 久久久久亚洲精品美女| 18禁美女裸体免费网站| 亚洲精品国产精品国自产网站 | 中文字幕亚洲第一| 中国一级特黄的片子免费 | 午夜国产羞羞视频免费网站| 美国毛片亚洲社区在线观看 | 亚洲AV无码国产精品色午友在线| 免费黄网站在线观看| 亚洲色图黄色小说| 啦啦啦高清视频在线观看免费 | 国产成人精品日本亚洲语音 | 亚洲成?v人片天堂网无码| 国产免费内射又粗又爽密桃视频 | 在免费jizzjizz在线播| 亚洲中文字幕乱码一区| 又粗又硬又大又爽免费视频播放| sihu国产精品永久免费| 精品亚洲麻豆1区2区3区| 最新猫咪www免费人成| 一级毛片在线免费播放| 久久久久亚洲精品天堂| 免费爱爱的视频太爽了| 中文字幕久无码免费久久| 亚洲色图校园春色| 日本高清免费不卡视频| a级在线观看免费| 自拍偷区亚洲国内自拍| 自拍偷自拍亚洲精品被多人伦好爽| 久艹视频在线免费观看| 亚洲欧美第一成人网站7777| 国产亚洲av片在线观看18女人| 99re热精品视频国产免费| 美女被免费网站在线视频免费|