Menu

25 Kasım 2010 Perşembe

Apache Digester Example

package dgstudent;

import java.util.ArrayList;
import java.util.List;

public class Academy {

    private List students;
    private List teachers;
    private String name;

    public Academy() {
        students = new ArrayList();
        teachers = new ArrayList();
    }

    public void addStudent(Student student) {
        students.add(student);
    }

    public void addTeacher(Teacher teacher) {
        teachers.add(teacher);
    }

    public List getStudents() {
        return students;
    }

    public void setStudents(List students) {
        this.students = students;
    }

    public List getTeachers() {
        return teachers;
    }

    public void setTeachers(List teachers) {
        this.teachers = teachers;
    }

    public String getName() {
        return name;
    }

    public void setName(String newName) {
        name = newName;
    }
}



package dgstudent;

public class Course {

    private String id;
    private String name;

    public Course() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


package dgstudent;

import java.util.ArrayList;
import java.util.List;

public class Student {

    private List courses;
    private String name;
    private String division;

    public Student() {
        courses = new ArrayList();
    }

    public void addCourse(Course course) {
        courses.add(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 List getCourses() {
        return courses;
    }

    public void setCourses(List courses) {
        this.courses = courses;
    }
}



package dgstudent;

import java.util.ArrayList;
import java.util.List;

public class Teacher {

    private String name;
    private List certifications;

    public Teacher() {
        certifications = new ArrayList();
    }

    public void addCertification(String certification) {
        certifications.add(certification);
    }

    public String getName() {
        return name;
    }

    public void setName(String newName) {
        name = newName;
    }

    public List getCertifications() {
        return certifications;
    }

    public void setCertifications(List certifications) {
        this.certifications = certifications;
    }
}

package dgstudent;

import java.net.URL;
import java.util.List;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;

public class DigestXMLJavaAcademy {

    public void digest() {
        try {
            //Create Digester using rules defined in academyRules.xml
            URL rules = DigestXMLJavaAcademy.class.getResource("./academy-rules.xml");
            Digester digester = DigesterLoader.createDigester(rules);
            //Parse academy.xml using the Digester to get an instance of Academy
            Academy a = (Academy) digester.parse(DigestXMLJavaAcademy.class.getResourceAsStream("./academy.xml"));

            List vStudents = a.getStudents();
            List vTeachers = a.getTeachers();
           
            for(Student s : vStudents){
                System.out.println(s.getCourses().get(0).getName());
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        DigestXMLJavaAcademy xmlDigest = new DigestXMLJavaAcademy();
        xmlDigest.digest();
    }
}