You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Aravinda Addala <ar...@majorband.co.uk> on 2001/12/04 15:54:44 UTC

Digester: Examples

Hi,

I am new to Digester. I have looked at the examples and they are great to
understand.

I want to create objects based on the properties.
My XML file contains several employees each has a ID as property. How can I
use digester to initialize a employee object based on ID?

My XML looks like this:

<?xml version="1.0"?>
<employees>
<employee id=1 firstName="First Name" lastName="Last Name">
  <address type="home" street="Home Street" city="Home City"
                        state="HS" zipCode="HmZip"/>
  <address type="office" street="Office Street" city="Office City"
                          state="OS" zipCode="OfZip"/>
</employee>

<employee id=2 firstName="First Name" lastName="Last Name">
  <address type="home" street="Home Street" city="Home City"
                        state="HS" zipCode="HmZip"/>
  <address type="office" street="Office Street" city="Office City"
                          state="OS" zipCode="OfZip"/>
</employee>

</employees>

Please help.

Regards,
Aravinda.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Digester: Examples

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 4 Dec 2001, Aravinda Addala wrote:

> Date: Tue, 4 Dec 2001 18:10:41 -0000
> From: Aravinda Addala <ar...@majorband.co.uk>
> Reply-To: Jakarta Commons Developers List <co...@jakarta.apache.org>
> To: Jakarta Commons Developers List <co...@jakarta.apache.org>
> Subject: Re: Digester: Examples
>
> Craig,
>
> Thank you for your reply.
>
> Sorry, I am still confused. How can I just initialize Employee with ID = 1?

Digester doesn't have any support for this -- it is designed to process
*all* the XML elements that match the rules you specify.  If this is
really what you want to do, you're probably better off doing your own XML
parse, using either the DOM or SAX APIs in JAXP.

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Digester: Examples

Posted by Aravinda Addala <ar...@majorband.co.uk>.
Craig,

Thank you for your reply.

Sorry, I am still confused. How can I just initialize Employee with ID = 1?

My xml contains SEVERAL employees.
When I call digester.parse() method, I get an object with all the employees.
I just want one Employee with the ID that I specify.

Regards,
Aravinda

----- Original Message -----
From: "Craig R. McClanahan" <cr...@apache.org>
To: "Jakarta Commons Developers List" <co...@jakarta.apache.org>
Sent: Tuesday, December 04, 2001 5:24 PM
Subject: Re: Digester: Examples


> On Tue, 4 Dec 2001, Aravinda Addala wrote:
>
> > Date: Tue, 4 Dec 2001 14:54:44 -0000
> > From: Aravinda Addala <ar...@majorband.co.uk>
> > Reply-To: Jakarta Commons Developers List
<co...@jakarta.apache.org>
> > To: commons-dev@jakarta.apache.org
> > Subject: Digester: Examples
> >
> > Hi,
> >
> > I am new to Digester. I have looked at the examples and they are great
to
> > understand.
> >
> > I want to create objects based on the properties.
> > My XML file contains several employees each has a ID as property. How
can I
> > use digester to initialize a employee object based on ID?
> >
> > My XML looks like this:
> >
> > <?xml version="1.0"?>
> > <employees>
> > <employee id=1 firstName="First Name" lastName="Last Name">
> >   <address type="home" street="Home Street" city="Home City"
> >                         state="HS" zipCode="HmZip"/>
> >   <address type="office" street="Office Street" city="Office City"
> >                           state="OS" zipCode="OfZip"/>
> > </employee>
> >
> > <employee id=2 firstName="First Name" lastName="Last Name">
> >   <address type="home" street="Home Street" city="Home City"
> >                         state="HS" zipCode="HmZip"/>
> >   <address type="office" street="Office Street" city="Office City"
> >                           state="OS" zipCode="OfZip"/>
> > </employee>
> >
> > </employees>
> >
> > Please help.
> >
> > Regards,
> > Aravinda.
> >
>
> Assume you have the following JavaBeans:
>
>     package com.mycompany.myapp;
>     public class Employee {
> public Employee() { ... }
> public int getId();
> public void setId(int id);
> public String getFirstName();
> public void setFirstName(String firstName);
> public String getLastName();
> public void setLastName(String lastName);
> public void addAddress(Address address);
>     }
>
>     package com.mycompany.myapp;
>     public class Address {
> public Address() { ... }
> ... getters and setters for "type", "street", "state", "zipCode" ...
>     }
>
> then the following rules should work fine:
>
>     digester.addObjectCreate("employee", "com.mycompany.myapp.Employee");
>     digester.addSetProperties("employee");
>     digester.addObjectCreate("employee/address",
>                      "com.mycompany.myapp.Address");
>     digester.addSetProperties("employee/address");
>     digester.addSetNext("employee/address", "addAddress",
>                         "com.mycompany.myapp.Address");
>
> The set properties rule will do conversions for all the native Java types,
> so an "id" property is no different than any other.  However, you will
> need to put quotes around your id values -- it's not valid XML syntax to
> have unquoted attribute values.
>
> Craig
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Digester: Examples

Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Tue, 4 Dec 2001, Aravinda Addala wrote:

> Date: Tue, 4 Dec 2001 14:54:44 -0000
> From: Aravinda Addala <ar...@majorband.co.uk>
> Reply-To: Jakarta Commons Developers List <co...@jakarta.apache.org>
> To: commons-dev@jakarta.apache.org
> Subject: Digester: Examples
>
> Hi,
>
> I am new to Digester. I have looked at the examples and they are great to
> understand.
>
> I want to create objects based on the properties.
> My XML file contains several employees each has a ID as property. How can I
> use digester to initialize a employee object based on ID?
>
> My XML looks like this:
>
> <?xml version="1.0"?>
> <employees>
> <employee id=1 firstName="First Name" lastName="Last Name">
>   <address type="home" street="Home Street" city="Home City"
>                         state="HS" zipCode="HmZip"/>
>   <address type="office" street="Office Street" city="Office City"
>                           state="OS" zipCode="OfZip"/>
> </employee>
>
> <employee id=2 firstName="First Name" lastName="Last Name">
>   <address type="home" street="Home Street" city="Home City"
>                         state="HS" zipCode="HmZip"/>
>   <address type="office" street="Office Street" city="Office City"
>                           state="OS" zipCode="OfZip"/>
> </employee>
>
> </employees>
>
> Please help.
>
> Regards,
> Aravinda.
>

Assume you have the following JavaBeans:

    package com.mycompany.myapp;
    public class Employee {
	public Employee() { ... }
	public int getId();
	public void setId(int id);
	public String getFirstName();
	public void setFirstName(String firstName);
	public String getLastName();
	public void setLastName(String lastName);
	public void addAddress(Address address);
    }

    package com.mycompany.myapp;
    public class Address {
	public Address() { ... }
	... getters and setters for "type", "street", "state", "zipCode" ...
    }

then the following rules should work fine:

    digester.addObjectCreate("employee", "com.mycompany.myapp.Employee");
    digester.addSetProperties("employee");
    digester.addObjectCreate("employee/address",
	                     "com.mycompany.myapp.Address");
    digester.addSetProperties("employee/address");
    digester.addSetNext("employee/address", "addAddress",
                        "com.mycompany.myapp.Address");

The set properties rule will do conversions for all the native Java types,
so an "id" property is no different than any other.  However, you will
need to put quotes around your id values -- it's not valid XML syntax to
have unquoted attribute values.

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>