1. 介绍
这是一个apache 用于解析XML的工具类,这里依赖到的包有以下几个
- commons-digester3-3.2.jar
- cglib-2.2.2.jar
- asm-3.3.1.jar
- commons-beanutils-1.8.3.jar
- commons-logging-1.1.1.jar
2.案例说明
下面就用一个实际的几个类来说明他的用法
测试类
import java.io.File;
import java.io.IOException;
import org.apache.commons.digester3.Digester;
import org.xml.sax.SAXException;
import com.io.test.apache.digester.model.DigesterRule;
import com.io.test.apache.digester.model.School;
import com.io.test.apache.digester.model.SchoolClass;
public class DigesterTest {
public static void main(String[] args) {
//创建一个Digester
Digester d = new Digester();
//添加规则组
d.addRuleSet(new DigesterRule());
try {
//开始解析xml文件,这列就是按照上面所设的规则组合来决定如何解析这个xml
School school = (School)d.parse(new File("./src/com/io/test/apache/digester/test.xml"));
System.out.println(school.getName());
for(SchoolClass sc : school.getSchoolClasses()){
System.out.println(sc.getClassName());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
规则组-1
import org.apache.commons.digester3.CallMethodRule;
import org.apache.commons.digester3.Digester;
import org.apache.commons.digester3.ObjectCreateRule;
import org.apache.commons.digester3.Rule;
import org.apache.commons.digester3.RuleSetBase;
import org.apache.commons.digester3.SetNestedPropertiesRule;
import org.apache.commons.digester3.SetNextRule;
public class DigesterRule extends RuleSetBase {
@Override
public void addRuleInstances(Digester arg0) {
//遇到shcool节点的时候,创建一个School对象
arg0.addRule("school", new ObjectCreateRule(School.class));
//遇到school/detail节点的时候,把子节点的内容存放在顶层对象(School)对象当中
arg0.addRule("school/detail", createSetNestedPropertiesRule());
//添加新的规则组,这里对应的是规则组-2
arg0.addRuleSet(new ClassRule());
//遇到school/classes/class节点的时候,执行addClass的时候方法,并且以上一个创建的对象作为参数传递,
arg0.addRule("school/classes/class", new SetNextRule("addClass"));
}
/**
* 在该指定路径下的所有节点,都会被设置到最顶端的对象当中
* @return
*/
public Rule createSetNestedPropertiesRule() {
SetNestedPropertiesRule rule = new SetNestedPropertiesRule();
rule.setAllowUnknownChildElements(true);
return rule;
}
/**
* 调用方法规则
* 指定路径下的节点获取到的值,会通过调用指定方法,执行,默认参数为String
* @param methodName
* @param paramCount
* @return
*/
public Rule createCallMethodRule(String methodName, int paramCount) {
return new CallMethodRule(methodName, paramCount);
}
}
规则组-2
import org.apache.commons.digester3.Digester;
import org.apache.commons.digester3.ObjectCreateRule;
import org.apache.commons.digester3.RuleSetBase;
import org.apache.commons.digester3.SetNestedPropertiesRule;
public class ClassRule extends RuleSetBase {
@Override
public void addRuleInstances(Digester arg0) {
//遇到school/classes/class节点的时候,创建一个schoolClass对象
arg0.addRule("school/classes/class", new ObjectCreateRule(SchoolClass.class));
//遇到school/classes/class节点的时候,把所有子节点的内容存放在顶层对象(schoolClass)的属性当中
arg0.addRule("school/classes/class", new SetNestedPropertiesRule());
}
}
其它模型类
学校模型
public class School {
private String name;
private String address;
private List<SchoolClass> schoolClasses;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addClass(SchoolClass schoolClass){
if(schoolClasses == null){
schoolClasses = new ArrayList<SchoolClass>();
}
this.schoolClasses.add(schoolClass);
}
public List<SchoolClass> getSchoolClasses() {
return schoolClasses;
}
}
班级模型
public class SchoolClass {
private String className;
private List<Student> students;
public SchoolClass() {
students = new ArrayList<Student>();
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
XML文件
<?xml version="1.0" encoding="utf-8" ?>
<school>
<detail>
<name>学校-1</name>
<address>北京</address>
</detail>
<classes>
<class>
<className>class-1</className>
</class>
</classes>
</school>
来源:oschina
链接:https://my.oschina.net/u/4285976/blog/3533660