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. SystemParametersInfo

    Python的模块pywin32中的win32gui.SystemParametersInfo()函数 在使用win32con.SPI_SETDESKWALLPAPER设置Wallpaper时,其第二 ...

  2. rt-thread博客分享

    对于理解rtos, 国内有一个rt-thread的开源社区,里面讲解了一些rtos的很多概念,方便了理解很多问题点,博客地址如下: http://www.cnblogs.com/King-Gentle ...

  3. byte数组和int互转

    import java.nio.ByteBuffer; public class Program { public static void main(String[] args) { ByteBuff ...

  4. [BZOJ]2132: 圈地计划 最小割

    圈地计划 Description 最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地.据了解,这块土地是一 ...

  5. 测试JS基本类型以及对象的引用

    自己敲的. 1 <script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js&qu ...

  6. .TextOut文字保存为图片

    //Canvas.TextOut文字保存为图片 //Delphi开发案例精选,使用TextOut在画布上画图procedure TForm1.Button1Click(Sender: TObject) ...

  7. (源)V8 Engine 编译

    v8 engine编译 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !imp ...

  8. Reviewing the Blog Module

    Reviewing the Blog Module Throughout the tutorial, we have created a fully functional CRUD module us ...

  9. 马上学Android开发在线视频教程全集

    马上学Android开发视频教程全集 马上学Android开发[马上学Android]安卓开发视频教程 001 Androi 马上学Android开发[马上学Android]安卓开发视频教程 002 ...

  10. ListView滑动不爽,滚动一页得滑几次?该用分页列表啦!

    ListView等滚动位置经常不符合用户期望: 很多时候都是看完一页想滑到下一页,但滑动一次距离往往不是不够就是超过,很难控制. PagedListView工程中提供了PageScroller来解决这 ...