在使用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的转换。
分享到:
相关推荐
提供处理XML、Directory、XADirectory的类库,提供从XML到JAVA类的转换机制。Castor(http://castor.exolab..org/)是一种将Java对象和XML自动绑定的开源软件。它可以在Java对象、XML文本、SQL数据表以及LDAP目录...
是个不错的XML解析工具。 1、xml格式文件的用法,我们学过的xml有两种用法:一,作为配置文件。二、作为数据存储文件。...2、这个工具的功能:能够把xml文件存储的数据和pojo对象进行映射。我们暂且把这种叫做:OXM。
Castor是ExoLab Group下面的一个开放源代码的项目,它主要实现的是O/R映射功能。它主要API和数据接口为:JDO-like, SQL, OQL, JDBC, LDAP, XML, DSML。它支持分布式目录事务处理和时间;提供处理XML、Directory、...
castor解析xml与xsd校验xml
castor Java与xml互转示例---代码 castor xml解析
javabean转换xml映射文件工具,可用于castor框架。JDK6编译、运行。
castor解析xml
castor比xstream实现对象与xml之间转化更强大些,利用mapping.xml文件规划,代码中相应工具类已经封装好,包名结构清晰,打入运行test包下java2string.java 和string2java.java可以测试对象之间转化
Castor 项目采用 BSD 类型的证书,因此可在任何类型的应用程序(包括完整版权的项目)中使用。 Castor 实际上仅仅有 XML 数据绑定,它还支持 SQL 和 LDAP 绑定
根据xml报文,一键自动生成castor插件maping文件和java对象,方便接口对接以及测试工具包测试说明文档
Castor实现XML与Java的互转,是目前非常流行的WSDL中常用的方法。有兴趣的可以下载看看
xml解析工具类,包含了一下的类: org.castor.util.IdentityMap org.castor.util.IdentitySet org.exolab.javasource.JEnum org.exolab.javasource.JType org.exolab.castor.util.List org.exolab.javasource.Header...
根据xml报文,一键自动生成castor插件maping文件和java对象
castor-0.9.5.3-xml.jar,java和xml互相转换所使用的JAR
Matlab类,用于连接Castor API。 警告:请确保您自己的客户ID和机密信息安全! 切勿在线(或离线)共享它们。 注意:我不隶属于Matlab或Castor。
Unmarshall与Marshall使用的castor-xml-1.3.2.jar包
要实现的是O/R映射功能。它主要API和数据接口为:JDO-like, SQL, OQL, JDBC, LDAP, XML, DSML。它支持分布式目录事务处理和时间;提供处理XML、Directory、XADirectory的类库,提供从XML到JAVA类的转换机制。
Castor的使用,如何将java object转换为xml与将xml转换为java object
解析wsdl文件的好帮手,和jdom.jar,wsdl4j.jar组合,能很好的解吸wsdl文件。
对于主要关心文档数据内容的应用程序,Java XML数据绑定是一种代替XML文档模型的强大机制。本文中将介绍数据绑定,并讨论什么使它如此令人瞩目。然后,展示如何利用Java数据绑定的开放源代码Castor框架来处理日益...