You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Valerio Schiavoni <va...@gmail.com> on 2005/12/28 15:58:06 UTC
[digester] different xml, same objective ?
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
Re: [digester] different xml, same objective ?
Posted by Valerio Schiavoni <va...@gmail.com>.
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) <di...@ef.pt>:
>
> 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: diogo.quintela@ef.pt
> PGP: 0xF51A5AB9
>
> > -----Original Message-----
> > From: Valerio Schiavoni [mailto:valerio.schiavoni@gmail.com]
> > 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 <ru...@hotmail.com>:
> > >
> > > 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: commons-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> > >
> > >
> >
> >
> > --
> > To Iterate is Human, to Recurse, Divine
> > James O. Coplien, Bell Labs
>
>
>
--
To Iterate is Human, to Recurse, Divine
James O. Coplien, Bell Labs
RE: [digester] different xml, same objective ?
Posted by "Diogo Quintela (EF)" <di...@ef.pt>.
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: diogo.quintela@ef.pt
PGP: 0xF51A5AB9
> -----Original Message-----
> From: Valerio Schiavoni [mailto:valerio.schiavoni@gmail.com]
> 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 <ru...@hotmail.com>:
> >
> > 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: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> >
>
>
> --
> To Iterate is Human, to Recurse, Divine
> James O. Coplien, Bell Labs
Re: [digester] different xml, same objective ?
Posted by Valerio Schiavoni <va...@gmail.com>.
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 <ru...@hotmail.com>:
>
> 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: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
--
To Iterate is Human, to Recurse, Divine
James O. Coplien, Bell Labs
Re: [digester] different xml, same objective ?
Posted by Valerio Schiavoni <va...@gmail.com>.
Hello Simon,
2005/12/30, Simon Kitching <sk...@apache.org>:
>
>
> 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
RE: [digester] different xml, same objective ?
Posted by Simon Kitching <sk...@apache.org>.
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: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
RE: [digester] different xml, same objective ?
Posted by Russell Simpkins <ru...@hotmail.com>.
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: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org