随笔-35  评论-97  文章-0  trackbacks-0

今天看了八进制的《利用DigesterXML转换为Java对象》,感觉挺好。你可以在这里http://www.cnblogs.com/bjzhanghao/archive/2007/05/15/125747.html访问。加上混北民工的在评论中更详细一点的例子。终于对Digester有了初步的了解。但是,对于详细返回对象,在八进制和混北民工的例子中表现得不是十分明朗。下面,我对例子做了一些修改,使Digester的规则表现清晰一些,另外添加配置规则来代替代码入注规则的运用方法。

 

注册表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>

 

配置规则设定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节点时 -->
        
<object-create-rule classname="study.jakarta.commons.digester.ChartRegistry" />  <!-- 创建对象 -->
        
<set-properties-rule /> <!-- 设置属性 -->
        
<pattern value="chart"> <!-- 遇上charts节点时 -->
            
<object-create-rule classname="study.jakarta.commons.digester.ChartConfig" /> <!-- 创建对象 -->
            
<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();
        }

    }

    
    
//代码中设定规则
    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());                         //创建一个ChartRegistry对象, 你这样用这种形式建立顶对象(选用这种方式,注释下一行)
                
//digester.addObjectCreate("charts", ChartRegistry.class);  //创建一个ChartRegistry对象, 你也这样用这种形式建立顶对象(选用这种方式,注释上一行)
                digester.addSetProperties("charts");
                digester.addObjectCreate(
"charts/chart", ChartConfig.class); //当遇到<chart>时创建一个ChartConfig对象
                digester.addSetProperties("charts/chart");  //根据<chart>元素的属性(attribute),对刚创建的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;
    }

    
    
//配置中设定规则
    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 三告习习 阅读(5274) 评论(2)  编辑  收藏 所属分类: Jakarta

评论:
# re: [jakarta-commons] 使用Digester解释xml获取java对象(代码入注规则 或 配置规则 任你选!) 2009-04-29 15:22 | 小虫旺福
写的很好。  回复  更多评论
  
# re: [jakarta-commons] 使用Digester解释xml获取java对象(代码入注规则 或 配置规则 任你选!) 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)  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: