commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Valerio Schiavoni <valerio.schiav...@gmail.com>
Subject [digester] different xml, same objective ?
Date Wed, 28 Dec 2005 14:58:06 GMT
hello, consider the following test case.
the code works fine for studentsXML1. how can I modify the digester part of
the test case to parse and read studentsXML2 and obtain the same result as
the previous one, that is to istantiate a Student object (which is not
posted because only containing setXXX/getXXX methods);

public class StudentDigester extends TestCase {

    protected String studentsXML1 = "<?xml version=\"1.0\"?>"
        + "<students>"
        + "<student>"
        + "<name>Java Boy</name>"
        + "<course>JSP</course>"
        + "<age>10</age>"
        + "</student>"
        + "</students>";

    protected String studentsXML2 = "<?xml version=\"1.0\"?>"
        + "<students>"
        + "<student>"
        + "<property name=\"name\" value=\"Java Boy\"/>"
        + "<property name=\"course\" value=\"Digester\"/>"
        + "<property name=\"age\" value=\"10\"/>"
        + "</student>"
        + "</students>";

    public void testAddBeanPropertySetter1() {

        DigestStudents ds = new DigestStudents(studentsXML1);
        ds.digest();

    }

    public void testAddBeanPropertySetter2() {

        DigestStudents ds = new DigestStudents(studentsXML2);
        ds.digest();

    }

    private class DigestStudents {
        List<Student> students;
        String input;
        public DigestStudents(String input) {

            students = new ArrayList<Student>();
            this.input = input;
        }

        private void digest() {
            try {
                Digester digester = new Digester();
                digester.setRules(new ExtendedBaseRules());

                digester.push(this);

                digester.addObjectCreate("students/student", Student.class);

                digester.addBeanPropertySetter("students/student/?");

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

                DigestStudents ds = (DigestStudents) digester.parse(new
StringReader(
                        input));

                System.out.println("Students List " + ds.students);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

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

}

thanks,
valerio
--
To Iterate is Human, to Recurse, Divine
James O. Coplien, Bell Labs

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message