[digester] different xml, same objective ?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[digester] different xml, same objective ?

Valerio
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
Reply | Threaded
Open this post in threaded view
|

RE: [digester] different xml, same objective ?

Russell Simpkins-2
given

>     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/?");

did you try:

digester.addBeanPropertySetter("students/student/property[name='name']/@value");
digester.addBeanPropertySetter("students/student/property[name='course']/@value");
digester.addBeanPropertySetter("students/student/property[name='age']/@value");

which is the XPath way to grab those property values



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] different xml, same objective ?

Valerio
Hello Russel,
i did try using this code:
digester.addBeanPropertySetter
("students/student/property[@name='name']/@value");
                digester.addBeanPropertySetter
("students/student/property[@name='course']/@value");
                digester.addBeanPropertySetter
("students/student/property[@name='age']/@value");

(i think you forgot a '@' before the name), but it doesn't build the Student
object properly.
this is the output i get:

Using studentXML1: Students List [Name=Java Boy & Course=JSP & Age=10]
Using studentXML2: Students List [Name=null & Course=null & Age=0]

where you can see that fields are not set properly.


2005/12/28, Russell Simpkins <[hidden email]>:

>
> given
>
> >     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/?");
>
> did you try:
>
> digester.addBeanPropertySetter
> ("students/student/property[name='name']/@value");
> digester.addBeanPropertySetter
> ("students/student/property[name='course']/@value");
> digester.addBeanPropertySetter
> ("students/student/property[name='age']/@value");
>
> which is the XPath way to grab those property values
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


--
To Iterate is Human, to Recurse, Divine
James O. Coplien, Bell Labs
Reply | Threaded
Open this post in threaded view
|

RE: [digester] different xml, same objective ?

Diogo Quintela (EF)
Shouldn't it be ?

digester.addBeanPropertySetter
("students/student/property[@name='name']/@value", "name");
digester.addBeanPropertySetter
("students/student/property[@name='course']/@value", "course");
digester.addBeanPropertySetter
("students/student/property[@name='age']/@value", "age");

If not, we may try to set property value to property named "value"



-----------------------------------
Diogo Bacelar Quintela
EF - Tecnologias de Informação, Lda.
Av. António Serpa, 26 - 4º Dto.
1050-027 Lisboa, Portugal
Tel: (+351) 217 827 800
Fax: (+351) 217 827 830
Email: [hidden email]
PGP: 0xF51A5AB9

> -----Original Message-----
> From: Valerio Schiavoni [mailto:[hidden email]]
> Sent: quarta-feira, 28 de Dezembro de 2005 17:05
> To: Jakarta Commons Users List
> Subject: Re: [digester] different xml, same objective ?
>
> Hello Russel,
> i did try using this code:
> digester.addBeanPropertySetter
> ("students/student/property[@name='name']/@value");
>                 digester.addBeanPropertySetter
> ("students/student/property[@name='course']/@value");
>                 digester.addBeanPropertySetter
> ("students/student/property[@name='age']/@value");
>
> (i think you forgot a '@' before the name), but it doesn't build the
> Student
> object properly.
> this is the output i get:
>
> Using studentXML1: Students List [Name=Java Boy & Course=JSP & Age=10]
> Using studentXML2: Students List [Name=null & Course=null & Age=0]
>
> where you can see that fields are not set properly.
>
>
> 2005/12/28, Russell Simpkins <[hidden email]>:
> >
> > given
> >
> > >     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/?");
> >
> > did you try:
> >
> > digester.addBeanPropertySetter
> > ("students/student/property[name='name']/@value");
> > digester.addBeanPropertySetter
> > ("students/student/property[name='course']/@value");
> > digester.addBeanPropertySetter
> > ("students/student/property[name='age']/@value");
> >
> > which is the XPath way to grab those property values
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>
>
> --
> To Iterate is Human, to Recurse, Divine
> James O. Coplien, Bell Labs

smime.p7s (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [digester] different xml, same objective ?

Valerio
thanks for the suggestion, but still doesn't working, reporting:
Using studentXML1: Students List [Name=Java Boy & Course=JSP & Age=10]
Using studentXML2: Students List [Name=null & Course=null & Age=0]


2005/12/28, Diogo Quintela (EF) <[hidden email]>:
>
> digester.addBeanPropertySetter
> ("students/student/property[@name='name']/@value", "name");
> digester.addBeanPropertySetter
> ("students/student/property[@name='course']/@value", "course");
> digester.addBeanPropertySetter
> ("students/student/property[@name='age']/@value", "age");
>
> If not, we may try to set property value to property named "value"



could you explain a bit more what do you mean by this ? the proposed
solution (apart from 'not working yet')  is some how too restrict, since it
requires to update the digester continuously when a new field on the student
object is created. the first approach leverages the developer to to do so,
and i was hoping to obtain the same result.


thanks,
valerio


-----------------------------------

> Diogo Bacelar Quintela
> EF - Tecnologias de Informação, Lda.
> Av. António Serpa, 26 - 4º Dto.
> 1050-027 Lisboa, Portugal
> Tel: (+351) 217 827 800
> Fax: (+351) 217 827 830
> Email: [hidden email]
> PGP: 0xF51A5AB9
>
> > -----Original Message-----
> > From: Valerio Schiavoni [mailto:[hidden email]]
> > Sent: quarta-feira, 28 de Dezembro de 2005 17:05
> > To: Jakarta Commons Users List
> > Subject: Re: [digester] different xml, same objective ?
> >
> > Hello Russel,
> > i did try using this code:
> > digester.addBeanPropertySetter
> > ("students/student/property[@name='name']/@value");
> >                 digester.addBeanPropertySetter
> > ("students/student/property[@name='course']/@value");
> >                 digester.addBeanPropertySetter
> > ("students/student/property[@name='age']/@value");
> >
> > (i think you forgot a '@' before the name), but it doesn't build the
> > Student
> > object properly.
> > this is the output i get:
> >
> > Using studentXML1: Students List [Name=Java Boy & Course=JSP & Age=10]
> > Using studentXML2: Students List [Name=null & Course=null & Age=0]
> >
> > where you can see that fields are not set properly.
> >
> >
> > 2005/12/28, Russell Simpkins <[hidden email]>:
> > >
> > > given
> > >
> > > >     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/?");
> > >
> > > did you try:
> > >
> > > digester.addBeanPropertySetter
> > > ("students/student/property[name='name']/@value");
> > > digester.addBeanPropertySetter
> > > ("students/student/property[name='course']/@value");
> > > digester.addBeanPropertySetter
> > > ("students/student/property[name='age']/@value");
> > >
> > > which is the XPath way to grab those property values
> > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [hidden email]
> > > For additional commands, e-mail: [hidden email]
> > >
> > >
> >
> >
> > --
> > To Iterate is Human, to Recurse, Divine
> > James O. Coplien, Bell Labs
>
>
>


--
To Iterate is Human, to Recurse, Divine
James O. Coplien, Bell Labs
Reply | Threaded
Open this post in threaded view
|

RE: [digester] different xml, same objective ?

Simon Kitching
In reply to this post by Russell Simpkins-2
On Wed, 2005-12-28 at 10:59 -0500, Russell Simpkins wrote:
> did you try:
>
> digester.addBeanPropertySetter("students/student/property[name='name']/@value");
> digester.addBeanPropertySetter("students/student/property[name='course']/@value");
> digester.addBeanPropertySetter("students/student/property[name='age']/@value");
>
> which is the XPath way to grab those property values

Unfortunately digester doesn't support xpath. There's no functionality
to do [...] or @... expressions, just pretty simple operations to match
or not match the "path to the current node".

However the syntax
  <student>
    <property name="prop1" value="val1"/>
    <property name="prop2" value="val2"/>
  </student>
can be processed using the SetPropertyRule.

Coinfiguring the Digester instance using:

  digester.addSetProperty("student/property", "name", "value");

will cause these calls:

  ObjOnTopOfStack.setProp1("val1");
  ObjOnTopOfStack.setProp2("val2");

which I believe is what is wanted.

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] different xml, same objective ?

Valerio
Hello Simon,


2005/12/30, Simon Kitching <[hidden email]>:
>
>
>   digester.addSetProperty("student/property", "name", "value");


this is exactly what  i was searching for. it works fine.

thanks a lot,
valerio



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