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. 【转载】svn代码回滚命令

    [说明]转载自 http://www.cnblogs.com/jndream/archive/2012/03/20/2407955.html 取消对代码的修改分为两种情况:   第一种情况:改动没有被 ...

  2. HTML-css selector

    Css selector 基本有三种 HTML(TAG)selector , ID selector , Class selector css selector 综合使用 : 重用,子选择器,组选择器 ...

  3. Project: Individual Project - Word frequency program----11061192zmx

    Description & Requirements http://www.cnblogs.com/jiel/p/3311400.html 项目时间估计 理解项目要求: 1小时 构建项目逻辑: ...

  4. 第十三章、学习 Shell Scripts

    什么是 Shell scripts shell script (程序化脚本) :shell script 是针对 shell 所写的『脚本!』 shell script 是利用 shell 的功能所写 ...

  5. VS2010 error C2664: “CWnd::MessageBoxW”: 不能将参数 1 从“const char [3]”转换为“LPCTSTR”

    VS2010 (VC2010)建立工程时默认的字符集是Unicode,所以在代码中使用MessageBox时需要输入Unicode,用TEXT()这个宏输入参数,否则会报错:   代码:Message ...

  6. Moebius实现Sqlserver集群~介绍篇

    今年是一个不平凡的一年,接触到了很多新艳的,让人兴奋的东西,虽然自己的牙掉了两颗,但感觉自己又年青了两岁,哈哈!进入正题,今年公司开始启用数据库集群,对于Sqlserver来说,实现方式并不是很多,一 ...

  7. iOS UDID和UUID详解

    这篇是普及知识来了,纯属消遣时间,有需要的可以遛一遛. UDID的全名为 Unique Device Identifier :设备唯一标识符.从名称上也可以看出,UDID这个东西是和设备有关的,而且是 ...

  8. AS问题解决系列1—Unable to execute DX错误

    http://my.oschina.net/1pei/blog/478968 摘要 在将一个开源Android代码import到Android Studio 1.2.2中时,解决了编译期间出现的“Un ...

  9. hdu 5455 Fang Fang 坑题

    Fang Fang Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5455 ...

  10. 通过IP控制登录系统

    项目中有这么一个需求,就是系统仅仅能在指定ip下登录,能够理解为内部系统,仅仅能够在公司訪问 我的代码是这样写的:入不入流不知道,但能解决这个问题. 获得訪问IP代码: String ip = req ...