看AjaxChat的时候看到Digester。自己也写了个程序测试一下。
student.xml:
<?xml version="1.0" encoding="UTF-8"?>
<teacher name="张三丰" course="java">
<student name="王八" age="20" id="1" />
<student name="张飞" age="22" id="2" />
<student name="李世民" age="21" id="3" />
<student name="李陵" age="22" id="4" />
<student name="高工" age="23" id="5" />
<student name="黄夺" age="24" id="6" />
<student name="马球" age="25" id="7" />
<student name="琚要" age="26" id="8" />
</teacher>
teacher.xml:
<?xml version="1.0" encoding="UTF-8"?>
<teachers>
<teacher name="张三丰" course="java">
<student>
<name>王八</name>
<age>20</age>
<id>1</id>
</student>
<student>
<name>张飞</name>
<age>22</age>
<id>1</id>
</student>
<student>
<name>李世民</name>
<age>21</age>
<id>3</id>
</student>
<student>
<name>李陵</name>
<age>22</age>
<id>4</id>
</student>
<student>
<name>高工</name>
<age>23</age>
<id>5</id>
</student>
<student>
<name>黄夺</name>
<age>24</age>
<id>6</id>
</student>
<student>
<name>马球</name>
<age>25</age>
<id>7</id>
</student>
<student>
<name>琚要</name>
<age>26</age>
<id>8</id>
</student>
</teacher>
<teacher name="李小龙" course="net">
<student>
<name>王八2</name>
<age>20</age>
<id>1</id>
</student>
<student>
<name>张飞2</name>
<age>22</age>
<id>1</id>
</student>
<student>
<name>李世民2</name>
<age>21</age>
<id>3</id>
</student>
<student>
<name>李陵2</name>
<age>22</age>
<id>4</id>
</student>
<student>
<name>高工2</name>
<age>23</age>
<id>5</id>
</student>
<student>
<name>黄夺2</name>
<age>24</age>
<id>6</id>
</student>
<student>
<name>马球2</name>
<age>25</age>
<id>7</id>
</student>
<student>
<name>琚要2</name>
<age>26</age>
<id>8</id>
</student>
</teacher>
</teachers>
package gjs.domain;
public class Student {
private int id;
private String name;
private int age;
//......
}
package gjs.domain;
import java.util.ArrayList;
import java.util.List;
public class Teacher {
private String name;
private String course;
private List<Student> students=new ArrayList<Student>();
public void addStudent(String name,String age, String id){
Student s=new Student();
s.setAge(Integer.valueOf(age));
s.setId(Integer.valueOf(id));
s.setName(name);
this.students.add(s);
}
public void addStudent(Student student){
this.students.add(student);
}
//......
}
package gjs.domain;
import java.util.ArrayList;
import java.util.List;
public class TeacherList {
private List<Teacher> teachers=new ArrayList<Teacher>();
public void addTeachers(Teacher teacher){
this.teachers.add(teacher);
}
public List<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(List<Teacher> teachers) {
this.teachers = teachers;
}
}
package gjs.test;
import java.io.File;
import java.io.IOException;
import gjs.domain.Student;
import gjs.domain.Teacher;
import gjs.domain.TeacherList;
import org.apache.commons.digester.Digester;
import org.junit.Test;
import org.xml.sax.SAXException;
import static org.junit.Assert.*;
public class DigesterTest {
/**
* 属性方式
*/
@Test public void testOne(){
Digester digester=new Digester();
//设置不进行合法性验证
digester.setValidating(false);
//当遇到<teacher>时创建一个Teacher对象,放入栈顶
digester.addObjectCreate("teacher", Teacher.class);
digester.addSetProperties("teacher");
//当遇到<teacher>的子元素<student>时创建一个Student对象,放入栈顶
digester.addObjectCreate("teacher/student", Student.class);
//设置<student>的属性值,根据属性值调用相应的set方法
digester.addSetProperties("teacher/student");
//当再次遇到<student>时创建一个Student对象,放入栈顶,同时调用栈次顶的addStudent方法
digester.addSetNext("teacher/student", "addStudent", "gjs.domain.Student");
try {
Teacher teacher = (Teacher)digester.parse(new File("src/student.xml"));
assertEquals(8, teacher.getStudents().size());
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
/**
* 子标签方式(设置Property方式一)
*/
@Test public void testTwo(){
Digester digester=new Digester();
digester.setValidating(false);
//设置栈顶
digester.push(new TeacherList());
digester.addObjectCreate("teachers/teacher", Teacher.class);
digester.addSetProperties("teachers/teacher");
digester.addObjectCreate("teachers/teacher/student", Student.class);
//子标签方式设置属性方法之一,第三个参数是第二个参数所指方法的参数个数
digester.addCallMethod("teachers/teacher/student/name", "setName",1);
digester.addCallMethod("teachers/teacher/student/age", "setAge",1);
digester.addCallMethod("teachers/teacher/student/id", "setId",1);
digester.addSetNext("teachers/teacher/student", "addStudent", "gjs.domain.Student");
digester.addSetNext("teachers/teacher", "addTeachers", "gjs.domain.Teacher");
try {
TeacherList teacher = (TeacherList)digester.parse(new File("src/teacher.xml"));
assertEquals(2, teacher.getTeachers().size());
assertEquals("java", teacher.getTeachers().get(0).getCourse());
assertEquals(8, teacher.getTeachers().get(0).getStudents().size());
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
/**
* 子标签方式(设置Property方式二)
*/
@Test public void testThree(){
Digester digester=new Digester();
digester.setValidating(false);
//设置栈顶
digester.push(new TeacherList());
digester.addObjectCreate("teachers/teacher", Teacher.class);
digester.addSetProperties("teachers/teacher");
digester.addObjectCreate("teachers/teacher/student", Student.class);
//子标签方式设置属性方法之二
digester.addBeanPropertySetter("teachers/teacher/student/name");
digester.addBeanPropertySetter("teachers/teacher/student/age");
digester.addBeanPropertySetter("teachers/teacher/student/id");
digester.addSetNext("teachers/teacher/student", "addStudent", "gjs.domain.Student");
digester.addSetNext("teachers/teacher", "addTeachers", "gjs.domain.Teacher");
try {
TeacherList teacher = (TeacherList)digester.parse(new File("src/teacher.xml"));
assertEquals(2, teacher.getTeachers().size());
assertEquals("java", teacher.getTeachers().get(0).getCourse());
assertEquals(8, teacher.getTeachers().get(0).getStudents().size());
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
/**
* 子标签方式 (callMethod callParam方式)
*/
@Test public void testFour(){
Digester digester=new Digester();
digester.setValidating(false);
digester.push(new TeacherList());
digester.addObjectCreate("teachers/teacher", "gjs.domain.Teacher");
digester.addSetProperties("teachers/teacher");
//当<student>时调用栈顶元素(Teacher)的addStudents却并不执行,等待addCallParam设置参数,3为参数个数,这里传入的参数是字符串。
digester.addCallMethod("teachers/teacher/student", "addStudent" ,3);
digester.addCallParam("teachers/teacher/student/name", 0);
digester.addCallParam("teachers/teacher/student/age", 1);
//这句之后即</student>才调用addStudents方法,并且如果再次遇到<student>重复。
digester.addCallParam("teachers/teacher/student/id", 2);
digester.addSetNext("teachers/teacher", "addTeachers", "gjs.domain.Teacher");
try {
TeacherList teacher=(TeacherList)digester.parse(new File("src/teacher.xml"));
assertEquals(2, teacher.getTeachers().size());
assertEquals("java", teacher.getTeachers().get(0).getCourse());
assertEquals(8, teacher.getTeachers().get(0).getStudents().size());
assertEquals("李世民2", teacher.getTeachers().get(1).getStudents().get(2).getName());
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}