commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject Re: Digester: Examples
Date Tue, 04 Dec 2001 17:24:40 GMT
On Tue, 4 Dec 2001, Aravinda Addala wrote:

> Date: Tue, 4 Dec 2001 14:54:44 -0000
> From: Aravinda Addala <aravinda_addala@majorband.co.uk>
> Reply-To: Jakarta Commons Developers List <commons-dev@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:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message