digester使用

一.序
  Digester是apache的一个组件 apache commons-digester.jar,通过它可以很方便的从xml文件生成java对象.你不用再象以前通过jdom或者Xerces去读取一个document对象.(jdom和Xerces仍然有它们的用武之地及强大之处,在其它应用里你也少不了它们)
我只是用一个例子展示一下如何把xml文件生成一个java object.
首先你必须把jar加入到你的项目classpath.
(一)准备:
1.环境: Eclipse, jdk1.4+
2.需求说明:学院包含学生和老师,学生可以选修课程,老师有不同的资格认证。
3.Java Class的定义,简洁起见,略去import内容。
4.使用Digester生成java对象 必须要定义一个xml文件,此xml文件必须符合digester-rules.dtd的规则。
5.所有的xml文件必须在classpath路径中,这样可以通过Class.getClassLoader().getInputStream()读取xml文件。

(二)提要:
  本示例所需的java class 和 xml文件分成四类:
1.基础xml文件: [academy.xml]: 你将要解析的xml文件.
2.基础java class: [Academy.java, Teacher.java, Student.java, Cousre.java]: 生成的对象的类定义
3.digesterRules.xml: [academyRules.xml]: 定义了基础xml文件到基础java class的匹配(映射)关系
4.Digester class: [DigestXMLJavaAcademy.java]: 根据digesterRules.xml去解析基础xml文件,并生成java 对象

(三)参考资料
   1.English ebook: Pro Jakarta Commons by Harshad Oak  Apress 2004

二.基础Java Class
1.学院 class Academy.java
    public class Academy {  
        private Vector students;  
        private Vector teachers;  
        private String name;  
      
        public Academy() {  
            students = new Vector();  
            teachers = new Vector();  
        }  
      
        public void addStudent(Student student) {  
            students.addElement(student);  
        }  
      
        public void addTeacher(Teacher teacher) {  
            teachers.addElement(teacher);  
        }  
      
        public Vector getStudents() {  
            return students;  
        }  
      
        public void setStudents(Vector newStudents) {  
            students = newStudents;  
        }  
        public Vector getTeachers() {  
            return teachers;  
        }  
      
        public void setTeachers(Vector newTeachers) {  
            teachers = newTeachers;  
        }  
      
        public String getName() {  
            return name;  
        }  
      
        public void setName(String newName) {  
            name = newName;  
        }  
    }  


2.学生 Student.java
    public class Student {  
        private Vector courses;  
        private String name;  
        private String division;  
      
        public Student() {  
            courses = new Vector();  
        }  
      
        public void addCourse(Course course) {  
            courses.addElement(course);  
        }  
      
        public String getName() {  
            return name;  
        }  
        public void setName(String newName) {  
            name = newName;  
        }  
      
        public String getDivision() {  
            return division;  
        }  
      
        public void setDivision(String newDivision) {  
            division = newDivision;  
        }  
      
        public void setCourses(Vector courses) {  
            this.courses = courses;  
        }  
      
        public Vector getCourses() {  
            return courses;  
        }  
    }  


3.老师 Teacher.java
    public class Teacher {  
        private String name;  
        private Vector certifications;  
      
        public Teacher() {  
            certifications = new Vector();  
        }  
      
        public void addCertification(String certification) {  
            certifications.addElement(certification);  
        }  
      
        public String getName() {  
            return name;  
        }  
      
        public void setName(String newName) {  
            name = newName;  
        }  
      
        public void setCertifications(Vector certifications) {  
            this.certifications = certifications;  
        }  
      
        public Vector getCertifications() {  
            return certifications;  
        }  
    }  



4.课程 Course.java
    public class Course {  
        private String id;  
        private String name;  
      
        public Course() {  
        }  
      
        public String getId() {  
            return id;  
        }  
      
        public void setId(String newId) {  
            id = newId;  
        }  
        public String getName() {  
            return name;  
        }  
      
        public void setName(String newName) {  
            name = newName;  
        }  
    }  



三.基础XML文件
============================================================
学院 academy.xml,包含了学生(包含了课程),老师
    <?xml version="1.0"?>  
    <academy name="JAcademy" >  
             <student name="JavaBoy" division="A">  
                      <course>  
                          <id>C1</id>  
                          <name>JSP</name>  
                      </course>  
                      <course>  
                          <id>C2</id>  
                          <name>Servlets</name>  
                      </course>  
             </student>  
             <student name="JavaGirl" division="B">  
                      <course>  
                          <id>C3</id>  
                          <name>EJB</name>  
                      </course>  
             </student>  
      
             <teacher name="JavaGuru">  
                      <certification>SCJP</certification>  
                      <certification>SCWCD</certification>  
             </teacher>  
             <teacher name="JavaMaster">  
                      <certification>OCP</certification>  
                      <certification>SCJP</certification>  
                      <certification>SCEA</certification>  
             </teacher>  
    </academy>  



四.Digester Class
DigestXMLJavaAcademy.java 和academyRules.xml
其中academyRules.xml用来定义xml文件和java对象的匹配(映射)关系,格式要符合digester-rules.dtd格式约束
DigestXMLJavaAcademy.java 根据academyRules.xml解析academy.xml文件并生成 Academy对象.
    1. academyRules.xml  
    <?xml version="1.0"?>  
    <digester-rules>  
      <pattern value="academy">  
          <object-create-rule classname="com.commonsbook.chap7.academy.Academy" />  
         <set-properties-rule />  
         <pattern value="student">  
             <object-create-rule classname="com.commonsbook.chap7.academy.Student" />  
             <set-properties-rule />  
      
             <pattern value="course">  
                 <object-create-rule classname="com.commonsbook.chap7.academy.Course" />  
                 <bean-property-setter-rule pattern="id"/>  
                 <bean-property-setter-rule pattern="name"/>  
                 <set-next-rule methodname="addCourse" />  
             </pattern>  
             <set-next-rule methodname="addStudent" />  
         </pattern>  
      
         <pattern value="teacher">  
             <object-create-rule classname="com.commonsbook.chap7.academy.Teacher" />  
             <set-properties-rule />  
             <call-method-rule pattern="certification" methodname="addCertification"  
                 paramcount="1" />  
             <call-param-rule pattern="certification" paramnumber="0"/>  
             <set-next-rule methodname="addTeacher" />  
         </pattern>  
     </pattern>  
    </digester-rules>  

2. DigestXMLJavaAcademy.java
    public class DigestXMLJavaAcademy {  
        public void digest(){  
            try {  
                //Create Digester using rules defined in academyRules.xml  
                Digester digester = DigesterLoader.createDigester(  
                         this.getClass().getClassLoader().getResource("academyRules.xml"));  
      
                //Parse academy.xml using the Digester to get an instance of Academy  
                Academy a = (Academy)digester.parse(  
                this.getClass().getClassLoader().getResourceAsStream("academy.xml"));  
      
                Vector vStud=a.getStudents();  
                Vector vTeach=a.getTeachers();  
      
                for (int i = 0; i < vStud.size(); i++) {  
                    System.out.println("Student>> "+PropertyUtils.describe(vStud.get(i)));  
                }  
      
                for (int i = 0; i < vTeach.size(); i++) {  
                    System.out.println("Teacher>> "+ PropertyUtils.describe(vTeach.get(i)));  
                }  
             } catch(Exception e) {  
                 e.printStackTrace();  
             }  
        }  
      
        public static void main(String[] args) {  
            DigestXMLJavaAcademy xmlDigest= new DigestXMLJavaAcademy();  
            xmlDigest.digest();  
        }  
    }  



我自己运行没成功,呵呵,继续努力吧,互勉。

















你可能感兴趣的:(java,Digester)