本文为转载且为截取的片段,如有需要请访问
原帖
待解析的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;
}
}
下面还有一个更详细的参考
http://onjava.com/pub/a/onjava/2002/10/23/digester.html
附件里放着rule.xml的dtd文件开放时放在rule.xml同一个目录即可
分享到:
相关推荐
开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2...
commons-logging-1.0-javadoc.jar, commons-logging-1.0.1-javadoc.jar, commons-logging-1.0.1.jar, commons-logging-1.0.2-javadoc.jar, commons-logging-1.0.2.jar, commons-logging-1.0.3-javadoc.jar, commons-...
commons-collections-20040616.jar, commons-collections-3.2-osgi.jar, commons-collections-3.2-sources.jar, commons-collections-3.2.1.jar, commons-collections-3.2.2-javadoc.jar, commons-collections-3.2.2...
赠送jar包:commons-net-3.8.0.jar; 赠送原API文档:commons-net-3.8.0-javadoc.jar; 赠送源代码:commons-net-3.8.0-sources.jar; 赠送Maven依赖信息文件:commons-net-3.8.0.pom; 包含翻译后的API文档:...
commons-pool-1.3.jar+commons-pool.jar;java连接池jar包java连接池;java连接池jar;commons-pool-1.2.jar;commons-pool-1.3.jar+commons-pool.jar;java连接池jar包java连接池;java连接池jar;commons-pool-1.2.jar;...
赠送jar包:commons-collections-3.2.2.jar; 赠送原API文档:commons-collections-3.2.2-javadoc.jar; 赠送源代码:commons-collections-3.2.2-sources.jar; 赠送Maven依赖信息文件:commons-collections-3.2.2....
开发工具 commons-dbcp2-2.1.1开发工具 commons-dbcp2-2.1.1开发工具 commons-dbcp2-2.1.1开发工具 commons-dbcp2-2.1.1开发工具 commons-dbcp2-2.1.1开发工具 commons-dbcp2-2.1.1开发工具 commons-dbcp2-2.1.1开发...
赠送jar包:commons-net-3.1.jar; 赠送原API文档:commons-net-3.1-javadoc.jar; 赠送源代码:commons-net-3.1-sources.jar; 包含翻译后的API文档:commons-net-3.1-javadoc-API文档-中文(简体)版.zip 对应...
在Java开发中,上传文件是一项常见的任务,而`commons-fileupload-1.3.3.jar`和`commons-io-2.6.jar`是Apache Commons项目中的两个重要库,专门用于处理HTTP请求中的文件上传功能。这两个库为开发者提供了便捷、高效...
本主题将详细解析标题和描述中提及的三个核心库:`commons-dbcp-1.2.1.jar`、`commons-pool-1.4.jar`以及`commons-collections-3.2.jar`,它们如何协同工作以提供高效的数据库连接管理服务。 1. **Apache Commons ...
赠送jar包:commons-compress-1.21.jar; 赠送原API文档:commons-compress-1.21-javadoc.jar; 赠送源代码:commons-compress-1.21-sources.jar; 赠送Maven依赖信息文件:commons-compress-1.21.pom; 包含翻译后...
赠送jar包:commons-fileupload-1.4.jar; 赠送原API文档:commons-fileupload-1.4-javadoc.jar; 赠送源代码:commons-fileupload-1.4-sources.jar; 赠送Maven依赖信息文件:commons-fileupload-1.4.pom; 包含...
赠送jar包:commons-beanutils-1.9.4.jar; 赠送原API文档:commons-beanutils-1.9.4-javadoc.jar; 赠送源代码:commons-beanutils-1.9.4-sources.jar; 赠送Maven依赖信息文件:commons-beanutils-1.9.4.pom; ...
commons-math3-3.6.1-API文档-中文版.rar commons-math3-3.6.1-API文档-中文版.rar commons-math3-3.6.1-API文档-中文版.rar commons-math3-3.6.1-API文档-中文版.rar commons-math3-3.6.1-API文档-中文版.rar ...
赠送jar包:commons-exec-1.3.jar; 赠送原API文档:commons-exec-1.3-javadoc.jar; 赠送源代码:commons-exec-1.3-sources.jar; 赠送Maven依赖信息文件:commons-exec-1.3.pom; 包含翻译后的API文档:commons-...
这里提到的三个JAR文件——`commons-beanutils.jar`、`commons-collections-3.1.jar`和`commons-pool-1.2.jar`,都是Apache Commons项目的一部分,分别涉及Bean操作、集合操作和对象池化。 **1. `commons-beanutils...
赠送jar包:commons-io-2.2.jar; 赠送原API文档:commons-io-2.2-javadoc.jar; 赠送源代码:commons-io-2.2-sources.jar; 包含翻译后的API文档:commons-io-2.2-javadoc-API文档-中文(简体)版.zip 对应Maven...
赠送jar包:commons-io-2.11.0.jar; 赠送原API文档:commons-io-2.11.0-javadoc.jar; 赠送源代码:commons-io-2.11.0-sources.jar; 赠送Maven依赖信息文件:commons-io-2.11.0.pom; 包含翻译后的API文档:...
赠送jar包:commons-io-2.8.0.jar; 赠送原API文档:commons-io-2.8.0-javadoc.jar; 赠送源代码:commons-io-2.8.0-sources.jar; 赠送Maven依赖信息文件:commons-io-2.8.0.pom; 包含翻译后的API文档:commons-io...