The requirement I have is
1) The value to be set for bean property is in XML.
2) Sometimes we need to extract data from multiple elements in the XML (essentially multiple XPath expressions) and run them through a translation to set a single bean property.
3) We don't want to use any XML data binding tools like JAXB or Castor as the schema is huge and we don't use most of the generated Java classes.
Input XML:
The beans to be populated areCode:<Root Element> <Lots of other XML elements that are not required> <Customer> <Name>Barack</Name> <Address> <Line1>1600</Line1> <Line2>Pennsylvania Ave</Line2> </Address> <Duration>2</Duration> </Customer> </Lots of other XML elements that are not required> </Root Element>
The expectation is to have the application context something like this.Code:public class Customer { private String name; private int duration; private Address address; //setter & getters } public class Address { private String addressLine; //setters & getters }
I have been reading about Custom Property Editors for doing this, but the issue is that property to be set in the bean could be of any data type and if I did something like thisCode:<beans> <bean id="customer" class="com.sample.Customer" > <property name="name" value="//Customer/Name/text()" /> <property name="duration" value="//Customer/Duration/text()" /> <property name="address" ref="address"/> </bean> <bean id="address" class="com.sample.Address"> <property name="addressLine" value="//Customer/Address/Line1/text(), //Customer/Address/Line2/text()" /> </bean> </beans>
that would be incorrect, because I would be overriding the default property editor for String and Integer and that would cause Spring to use theCode:public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar { public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(String.class, new XPathPropertyEditor(String.class)); registry.registerCustomEditor(Integer.class, new XPathPropertyEditor(Integer.class)); } }
custom XPathPropertyEditor if the value for a bean property was in the application context xml.
Is there a better way to do this. Any help is appreciated.


Reply With Quote