`
- 浏览:
451277 次
- 性别:
- 来自:
深圳
-
java 代码:
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
]]
]
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Digester提供了两种主要的解析方式,即规则硬编码和独立的规则文件,这两种方法各有其特点和适用场景。 1. 规则硬编码: 在硬编码方式中,开发者直接在代码中创建和设置Digester的规则。这些规则定义了如何处理XML...
Digester是Apache Commons项目的一个组件,它的核心思想是通过匹配XML文档中的元素路径,触发相应的处理规则(Rule)。这些规则可以用来创建和初始化Java对象,设置对象的属性,甚至执行更复杂的操作。这样,一个XML...
Apache Commons Digester是Java中一个强大的工具,它提供了方便的方式来解析XML文档,并将解析出的数据绑定到Java对象上,实现对象的自动创建和初始化。本示例将详细介绍如何使用Apache Commons Digester进行XML解析...
在Java开发中,处理XML数据是一项常见的任务,而Apache Commons Digester库就是这样一个强大的工具,它提供了一种高效且方便的方式来解析XML文档,并将其映射到Java对象结构中。本文将深入探讨digester包的原理、...
在Java中,解析XML文件通常有两种主要方法:DOM(Document Object Model)和SAX(Simple API for XML)。DOM将整个XML文档加载到内存中,形成一个树形结构,便于遍历和修改;而SAX是一种事件驱动模型,只读且逐行...
例如,XML文件中描述了一个Person对象,包含name和age属性, Digester可以通过如下方式解析: ```xml <name>John Doe <age>30 ``` 对应的Java代码可能如下: ```java Digester digester = new Digester(); ...