digeststudents.java

来自「采用Jakatta Apache Commons的Digester解析XML文档」· Java 代码 · 共 54 行

JAVA
54
字号
package wwt.helloDigester.sample;
import java.util.Vector;
import org.apache.commons.digester.Digester;

public class DigestStudents {
    Vector students;

    public DigestStudents() {
        students= 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.push(this);

            //Creates a new instance of the Student class
            digester.addObjectCreate("students/student", Student.class);

            //Uses setName method of the Student instance
            //Uses tag name as the property name
            digester.addBeanPropertySetter("students/student/name");

            //Uses setCourse method of the Student instance
            //Explicitly specify property name as 'course'
            digester.addBeanPropertySetter("students/student/course", "course");

            //Move to next student
            digester.addSetNext("students/student", "addStudent");

            DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
                                  .getClassLoader()
                                  .getResourceAsStream("students.xml"));

            //Print the contents of the Vector
            System.out.println("Students Vector "+ds.students);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void addStudent(Student stud) {
         //Add a new Student instance to the Vector
         students.add(stud);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?