首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

Castor施用之:映射Clob字段到xml文件

2012-11-17 
Castor使用之:映射Clob字段到xml文件在使用Castor将java对象转换为xml文件时,会遇到Clob类型的属性的转换。

Castor使用之:映射Clob字段到xml文件
在使用Castor将java对象转换为xml文件时,会遇到Clob类型的属性的转换。Castor中没有Clob类型的处理器,因此需要我们自己实现一个ClobFieldHandler。

例如:我们要将Root.java转换成一个xml文件。

1.Root.java

package lab.castor.lab01;import java.sql.Clob;public class Root {private Clob comment;public Clob getComment() {return comment;}public void setComment(Clob comment) {this.comment = comment;}}


2.mapping.xml

<?xml version="1.0"?><mapping>  <class name="lab.castor.lab01.Root">     <field name="comment" type="string" handler="lab.castor.lab01.ClobFieldHandler">        <bind-xml node="comment"/>     </field>  </class></mapping>



3.ClobFieldHandler.java

package lab.castor.lab01;import java.io.Reader;import java.sql.Clob;import org.exolab.castor.mapping.GeneralizedFieldHandler;public class ClobFieldHandler extends GeneralizedFieldHandler {/** * Creates a new ClobFieldHandler instance */public ClobFieldHandler() {super();}/** * This method is used to convert the value when the getValue method is called. The getValue method will obtain the * actual field value from given 'parent' object. This convert method is then invoked with the field's value. The * value returned from this method will be the actual value returned by getValue method. *  * @param value the object value to convert after performing a get operation * @return the converted value. */public Object convertUponGet(Object value) {if (value == null) {return null;}StringBuffer sb = new StringBuffer();try {Clob clob = (Clob) value;Reader reader = clob.getCharacterStream();if (reader == null) {return null;}char[] charbuf = new char[4096];// 解决4096字节大小的限制for (int i = reader.read(charbuf); i > 0; i = reader.read(charbuf)) {sb.append(charbuf, 0, i);}} catch (Exception e) {//}return sb.toString();}/** * This method is used to convert the value when the setValue method is called. The setValue method will call this * method to obtain the converted value. The converted value will then be used as the value to set for the field. *  * @param value the object value to convert before performing a set operation * @return the converted value. */public Object convertUponSet(Object value) {return null;}/** * Returns the class type for the field that this GeneralizedFieldHandler converts to and from. This should be the * type that is used in the object model. *  * @return the class type of of the field */public Class getFieldType() {return Clob.class;}/** * Creates a new instance of the object described by this field. *  * @param parent The object for which the field is created * @return A new instance of the field's value * @throws IllegalStateException This field is a simple type and cannot be instantiated */public Object newInstance(Object parent) throws IllegalStateException {// -- Since it's marked as a string...just return null,// -- it's not needed.return null;}}


没有实现String到Clob的转换。

热点排行