⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 digeststudents.java

📁 采用Jakatta Apache Commons的Digester解析XML文档简单例程,附带有ant构建文件,可以做为程序开发中XML文件解析的常用参考
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -