The annotation XmlSchemaType defines a mapping between an arbitrary Java type and a simple schema built-in type. Most of the time the default mapping is satisfactory, but every now and then an alternative may be more convenient. Let's assume that the processing of chunks of text requires their extension, either after unmarshalling or before the emitting marshalling. For this, a StringBuffer is better than String, which is the default mapping for xs:string. Below are the essential Java classes, one defining TextType as a container for a string, and the type adapter class for the simple conversion between String and StringBuffer. Notice that the latter class is specified in a separate annotation, i.e.,XmlJavaTypeAdapter.

public class TextType {
@XmlElement
@XmlSchemaType(name="string")
@XmlJavaTypeAdapter( String2StrBuf.class )
public StringBuffer strbuf;
} public class String2StrBuf
extends XmlAdapter<String,StringBuffer> {
@Override
public String marshal( StringBuffer strbuf ){
return strbuf.toString();
}
@Override
public StringBuffer unmarshal( String string ){
return new StringBuffer( string );
}
}

Within the Java code that unmarshals or marshals an instance document, TextType elements are now StringBuffers, e.g.:

TextType text = new TextType();
text.strbuf = new StringBuffer( "This is the house" );
// ...
text.strbuf.append( " that Jack built." );

Such a type mapping can be defined either for an individual element or for all occurrences within a package. If you need multiple mappings at package level, you'll have to bundle theXmlSchemaType annotations in an XmlSchemaTypes (note the plural) annotation, and the XmlJavaTypeAdapter annotiations are packed into a single XmlJavaTypeAdapters annotation.

JAXB - Annotations, Type Mapping: XmlSchemaType的更多相关文章

  1. JAXB - Annotations, Type Adapters: XmlJavaTypeAdapter

    For some Java container types JAXB has no built-in mapping to an XML structure. Also, you may want t ...

  2. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-007UserTypes的用法(@org.hibernate.annotations.Type、@org.hibernate.annotations.TypeDefs、CompositeUserType、DynamicParameterizedType、、、)

    一.结构 二.Hibernate支持的UserTypes接口  UserType —You can transform values by interacting with the plain JD ...

  3. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-005控制类型映射(Nationalized、@LOB、@org.hibernate.annotations.Type)

    一.简介 1. 2. 3. 4. to override this default mapping. The JPA specification has a convenient shortcut a ...

  4. JAXB - Annotations, Top-level Elements: XmlRootElement

    A class that describes an XML element that is to be a top-level element, i.e., one that can function ...

  5. JAXB - Annotations, Controlling Element Selection: XmlAccessorType, XmlTransient

    If JAXB binds a class to XML, then, by default, all public members will be bound, i.e., public gette ...

  6. JAXB - Annotations, The Annotation XmlElement

    The basic annotation for a field that's intended to be an element is XmlElement. It permits you to d ...

  7. Mysql Java type mapping

    MySQL Type Java Type ---------- --------- CHAR String VARCHAR String LONGVARCHAR String NUMERIC java ...

  8. JAXB - Annotations, Annotation for Classes: XmlType

    This annotation adds information that would be available from a schema type, but isn't implied by a ...

  9. JAXB - Annotations, Annotations for the Schema: XmlSchema

    This annotation can only be used with a package. It defines parameters that are derived from the xsd ...

随机推荐

  1. Storm入门

    Storm框架入门 1 Topology构成 和同样是计算框架的Mapreduce相比,Mapreduce集群上运行的是Job,而Storm集群上运行的是Topology.但是Job在运行结束之后会自 ...

  2. HW6.27

    import java.util.Scanner; import java.util.Arrays; public class Solution { public static void main(S ...

  3. JDBC注册驱动的三种方式

    JDBC注册驱动的三种方式 1.通过导入的JDBC的驱动包拿到的com.mysql.jdbc.Driver对象,利用java.sql.DriverManager对象的DriverManager.reg ...

  4. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  5. css div 不能贴边

    *{margin:0;padding:0} 在css 最前加上这句,取消所有内边距和外边距.

  6. Java网络编程(TCP协议-服务端和客户端交互)

    客户端: package WebProgramingDemo; import java.io.IOException; import java.io.InputStream; import java. ...

  7. 查找进程对应的PID和对应的端口号

    第一步:首先打开任务管理器.之后左键单击查看,点击下面的选择列. 第二步:之后进入如下界面,把PID勾上.这是我们就可以查看到进程的PID(process id)号了 第三步:首先我们打开DOS窗口, ...

  8. SQL2008-功能设置

    1.问题:修改表字段类型后,无法保存答:工具—选项—Designers—表设计器和数据库设计器—阻止保存要求重新创建表的更改前的勾去掉. 2.问题: 编写SQL语句的地方怎么自动换行答:工具-> ...

  9. 多进程解决datasnap支持的tcp长连接数量少的问题

    对于实时采集数据的项目,应用场景比如是这样的:5000客户端,每个客户端每隔500MS要给服务器上传一次数据. 大家知道,像INDY这种阻塞型的通信控件,所能支持的TCP长连接的一般地不能超过1000 ...

  10. SQLite使用教程8 Insert 语句

    http://www.runoob.com/sqlite/sqlite-insert.html SQLite Insert 语句 SQLite 的 INSERT INTO 语句用于向数据库的某个表中添 ...