Digester不是一個XML Parser,它只是對SAX更高層次上的一個封裝使用Digester,將XML映射成javaBean. 我們無須了解SAX和DOM的解析過程,只要給Digester添加一些解析規則,就能對一個xml文件進行解析。Digester使用堆棧來保存xml節點(stack.push()方法),當該xml節點中嵌套的所有子節點解析完畢,該節點將被彈出(stack.pup()方法)。Digester最大的優點就是使用模式匹配來表示xml文件中節點之間的父子關系 。
代碼如下:
students.xml
<?xml version="1.0" encoding="UTF-8" ?>
<stuClass name="fddfdf">
<student>
<name from="cn">Java Boy</name>
<course>JSP</course>
</student>
<student>
<name>Java Girl</name>
<course>EJB</course>
</student>
</stuClass>
Model類:Student.java
package demo.javabean;
public class Student ...{
private String name;
private String from;
private String course;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getFrom() ...{
return from;
}
public void setFrom(String from) ...{
this.from = from;
}
public String getCourse() ...{
return course;
}
public void setCourse(String course) ...{
this.course = course;
}
}
Model類:StuClass.java
package demo.javabean;
import java.util.Vector;
public class StuClass ...{
private String name;
private Vector students = new Vector();
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public Vector getStudents() ...{
return students;
}
public void setStudents(Vector students) ...{
this.students = students;
}
public void addStudent(Student student)...{
students.add(student);
}
}
測試類:DigestStudents.java
package demo;
import java.util.Vector;
import org.apache.commons.digester.Digester;
import demo.javabean.StuClass;
public class DigestStudents ...{
Vector stuClass;
public DigestStudents() ...{
stuClass = new Vector();
}
public static void main(String[] args) ...{
DigestStudents digestStudents = new DigestStudents();
digestStudents.digest();
}
private void digest() ...{
try ...{
Digester digester = new Digester();
// Push the current object onto the stack
digester.setValidating(false);
// Creates a new instance of the Student class
digester.addObjectCreate("stuClass", "demo.javabean.StuClass");
digester.addSetProperties("stuClass", "name", "name");
digester.addObjectCreate("stuClass/student", "demo.javabean.Student");
// Uses setName method of the Student instance
// Uses tag name as the property name
// addCallMethod與addBeanPropertySetter等價
// 參數 0代表一個參數,默認就是當前讀的數據,最後一個參數0表示參數個數
digester.addCallMethod("stuClass/student/name", "setName", 0);
digester.addSetProperties("stuClass/student/name", "from", "from");
// 加上一個屬性名form
// digester.addBeanPropertySetter( "stuClass/student/name");
// Uses setCourse method of the Student instance
// Explicitly specify property name as 'course'
digester.addBeanPropertySetter("stuClass/student/course");
// Move to next student,addStudent為其中的一個方法
digester.addSetNext("stuClass/student", "addStudent",
"demo.javabean.Student");
StuClass ds = (StuClass) digester.parse(this.getClass()
.getClassLoader().getResourceAsStream(
"demo/students.xml"));
// Print the contents of the Vector
System.out.println("Students Vector " + ds.getName());
System.out.println("Students Vector " + ds.getStudents());
} catch (Exception ex) ...{
ex.printStackTrace();
}
}
// public void addStudent( Student stud ) {
// //Add a new Student instance to the Vector
// stuClass.add( stud );
// }
}