Java Reference
In-Depth Information
<class name="com.apress.springenterpriserecipes.weather.TemperatureInfo">
<map-to xml="TemperatureInfo"
ns-uri=" http://springenterpriserecipes.apress.com/weather/schemas" />
<field name="city" type="string">
<bind-xml name="city" node="attribute" />
</field>
<field name="date" type="string"
handler="com.apress.springenterpriserecipes.weather.DateFieldHandler">
<bind-xml name="date" node="attribute" />
</field>
<field name="min" type="double">
<bind-xml name="min" node="element" />
</field>
<field name="max" type="double">
<bind-xml name="max" node="element" />
</field>
<field name="average" type="double">
<bind-xml name="average" node="element" />
</field>
</class>
</mapping>
Remember that for each class mapping, you must specify the namespace URI for the element.
Besides, for all the date fields, you have to specify a handler to convert the dates with a particular date
format. The handler is implemented as shown following:
package com.apress.springenterpriserecipes.weather;
...
import org.exolab.castor.mapping.GeneralizedFieldHandler;
public class DateFieldHandler extends GeneralizedFieldHandler {
private DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
public Object convertUponGet(Object value) {
return format.format((Date) value);
}
public Object convertUponSet(Object value) {
try {
return format.parse((String) value);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
public Class getFieldType() {
return Date.class;
}
}
Search WWH ::




Custom Search