commons-digest笔记

本文为转载且为截取的片段,如有需要请访问[url=http://www.iteye.com/topic/39330]原帖[/url]

待解析的xml

<?xml version="1.0"?>
<academy name="JAcademy">
<student name="JavaBoy" division="A">
<course>
<id>C1</id>
<name>JSP</name>
</course>
<course>
<id>C2</id>
<name>Servlets</name>
</course>
</student>
<student name="JavaGirl" division="B">
<course>
<id>C3</id>
<name>EJB</name>
</course>
</student>
<teacher name="JavaGuru">
<certification>SCJP</certification>
<certification>SCWCD</certification>
</teacher>
<teacher name="JavaMaster">
<certification>OCP</certification>
<certification>SCJP</certification>
<certification>SCEA</certification>
</teacher>
</academy>



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE digester-rules PUBLIC
"-//Jakarta Apache //DTD digester-rules XML V1.0//EN"
"digester-rules.dtd">
<digester-rules>
<pattern value="academy">
<object-create-rule classname="cn.com.legendapl.xmldigest.config.ConfigBean" />
<set-properties-rule />

<pattern value="student">
<object-create-rule classname="cn.com.legendapl.xmldigest.config.StudentBean" />
<set-properties-rule />
<pattern value="course">
<object-create-rule classname="cn.com.legendapl.xmldigest.config.CourseBean" />
<set-properties-rule />
<bean-property-setter-rule pattern="id" />
<bean-property-setter-rule pattern="name" />
<set-next-rule methodname="addCourseBean" />
</pattern>
<set-next-rule methodname="addStudentBean" />
</pattern>

<pattern value="teacher">
<object-create-rule classname="cn.com.legendapl.xmldigest.config.TeacherBean" />
<set-properties-rule />
<call-method-rule pattern="certification" methodname="addCertification" paramcount="1"/>
<call-param-rule pattern="certification" paramnumber="0"/>
<set-next-rule methodname="addTeacherBean" />
</pattern>
</pattern>
</digester-rules>



四个配置类。为了简单和节约博客的空间,getter/setter已删去。

public class AcademyBean {

private String name;
private List<StudentBean> studentList = new ArrayList<StudentBean>();
private List<TeacherBean> teacherList = new ArrayList<TeacherBean>();

public static URL getRuleFile() {
return AcademyBean.class.getClassLoader().getResource("cn/com/legendapl/xmldigest/config/rule.xml");
}

public void addTeacherBean(TeacherBean bean) {
this.teacherList.add(bean);
}

public void addStudentBean(StudentBean bean) {
this.studentList.add(bean);
}
}

public class StudentBean {

private String name;
private String division;

private List<CourseBean> courseBeans = new ArrayList<CourseBean>();

public void addCourseBean(CourseBean bean) {
courseBeans.add(bean);
}
}

public class CourseBean {
private String id;
private String name;
}

public class TeacherBean {

private String name;
private Set<String> certifications = new HashSet<String>();

public void addCertification(String certification) {
this.certifications.add(certification);
}
}


读取配置的工具类也可以做一个

public final class ConfigUtils {

private ConfigUtils() {}

private static AcademyBean config = null;

public static AcademyBean getConfig() {
if (config == null) {
Digester digester = DigesterLoader.createDigester(AcademyBean.getRuleFile());
try {
config = (AcademyBean)digester.parse(ConfigUtils.class.getClassLoader().getResource("academy.xml"));
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
return config;
}

}



下面还有一个更详细的参考
[url]http://onjava.com/pub/a/onjava/2002/10/23/digester.html[/url]

附件里放着rule.xml的dtd文件开放时放在rule.xml同一个目录即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值