commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "" <>
Subject Re: How to create a Java object if the xml element is in wrong place?
Date Thu, 24 Apr 2008 13:01:15 GMT
Hi Robert,

Robert Lee schrieb:
> I am parsing an xml file to the digester xml rules to create objects.
> The xml file we've got does not confront to the java model correctly. The
> address and contact elements are under the person. The correct format would
> have been the address element to be inside the contact.
> Unfortunately I can not change the format of the xml but I have to find a
> way using xmlrule to overcome this problem.
> The format of the xml file is:
> <root>
>             <person>
>                         <address line1="29  Glen Street" line2="Tamara"
> postcode="XYZ123"></address>
>                         <contact telephoneno="0123456789"
> emailaddress=""></contact>     
>             </person>
> </root>
> The Java has been defined as:
> -----------------
> Contact contact;
> public void setContact(Contact contact) { = contact; }
> public Contact getContact() { return contact; }
> -----------------
> Address address
> public void setAddress(Address address) { this.address = address; }
> public Address getAddress() { return address; }
> -----------------
> private String line1;
> private String line2;
> private String postcode;
> setters/getters for line1,line2 and postcode;
> my xml rule is:
> <pattern value="root/person">
>             <object-create-rule classname="" />
>             <set-properties-rule />
>             <set-next-rule methodname="setContact" />
> </pattern>
> <pattern value="root/person/contact">
>             <object-create-rule classname="" />
>             <set-properties-rule />
>             <set-next-rule methodname="setAddress" />
> </pattern>
> <pattern value="root/person/address">
>            <!-- How to write my rule here -->
>             <object-create-rule classname="" />
>             <set-properties-rule />
>             <set-next-rule methodname="setAddress" />
> </pattern>
> If I add setAddress in then it works fine but setAddress is only
> defined in
> How can I write my address rule so that it work?

Does your input xml really have "address" elements *before* contact
elements? If so, that will be hard to handle; the data needs to be added
to a contact object that does not yet exist!

If this is just a typing error, or you can change the xml so the contact
object that is the parent of the address occurs *before* the address
then this would be easier. In this case, I would use a custom Rule class
that triggers on "root/person/address", which just fetches the needed
attributes and then calls Person.getContact to find the contact object
to add the address info to.

If the xml is really as you describe, then you will need to store the
address info somewhere until the Contact object gets created (and
somehow deal with the situation where a Person has <address> elements,
but no <contact> element. Again, it's probably easiest to just write a
custom Rule class to handle "root/person/address", which stores this
data using Digester.push(stackname, data). Then you would need another
custom rule in order to copy the data you saved earlier into the
relevant Contact object, mapped to fire on "root/person/contact" or
"root/person" (or both).

Don't be concerned about writing custom Rule classes; it is quite easy
and there is absolutely nothing wrong with doing that. The Rule classes
provided with digester are just a useful starting set, and don't cover
every case.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message