For some Java container types JAXB has no built-in mapping to an XML structure. Also, you may want to represent Java types in a way that is entirely different from what JAXB is apt to do. Such mappings require an adapter class, written as an extension of XmlAdapter<XmlType,ApplType> from the package javax.xml.bind.annotation.adapters. The annotation XmlJavaTypeAdapter is provided for announcing the adapter in the desired place.

We'll illustrate adapters by defining a substitution of a map for an array. Here is an XML example of the data we have to deal with.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns:training xmlns:ns="http://foo/bar">
<brochure>
<course price="123.45" id="c1">
<name>Course 1</name>
</course>
<course price="123.45" id="c0">
<name>Course 0</name>
</course>
</brochure>
</ns:training>

The course elements could be represented as a list or array, but we would like to process this data in our application as a map of the id attribute to the Course object. Although JAXB is capable of handling maps, we have seen (in section Top-level Elements: XmlRootElement) that the resulting XML structure isn't as simple as possible. To achieve our goal, we write a class Brochure containing the map we have in mind and declare that this is the one that has to be adapted to something JAXB knows how to handle, i.e., the classCourses containing a simple array of Course objects.

@XmlRootElement(name="training")
public class Training {
@XmlElement
public Brochure brochure;
public Training(){}
public Training( Brochure b ){
brochure = b;
}
} @XmlJavaTypeAdapter(BrochureAdapter.class)
public class Brochure {
Map<String,Course> courses;
public Brochure() {
courses = new HashMap<String, Course>();
}
} public class Courses {
@XmlElement(name="course")
public Course[] carray;
} public class Course {
@XmlAttribute
String id;
@XmlElement
String name;
@XmlAttribute
Price price;
}

Class Brochure is annotated with XmlJavaTypeAdapter, defining class BrochureAdapter as its adapter, and this is, of course, the interesting class. It has to override methodsunmarshal and marshal.

public class BrochureAdapter extends XmlAdapter<Courses,Brochure> {
@Override
public Brochure unmarshal( Courses value ){
Brochure b = new Brochure();
for( Course c : value.carray )
b.courses.put( c.id, c );
return b;
} @Override
public Courses marshal( Brochure b ){
Courses courses = new Courses();
Collection<Course> c = b.courses.values();
courses.carray = c.toArray(new Course[c.size()]);
return courses;
}
}

Courses is a class JAXB knows how to handle with respect to XML data, and the result of JAXB's innate capabilities is passed to the adaption for unmarshalling. In this method, we convert the data to a structure according to the desired class Brochure with its map. The reverse marshalling process has to convert a Brochure object with its map to a Courses object, which is easily done by putting the map values into an array.

To summarize: XML binding happens against the class Courses, whereas application programming uses the Map type field courses in class Brochure.

JAXB - Annotations, Type Adapters: XmlJavaTypeAdapter的更多相关文章

  1. JAXB - Annotations, Type Mapping: XmlSchemaType

    The annotation XmlSchemaType defines a mapping between an arbitrary Java type and a simple schema bu ...

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

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

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

  7. JAXB - Annotations, Annotation for Classes: XmlType

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

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

  9. JAXB - Annotations, The Object Factory: XmlRegistry, XmlElementDecl

    To be able to create objects from XML elements, the unmarshaller must have an object factory with me ...

随机推荐

  1. poj 1704 阶梯博弈

    转自http://blog.sina.com.cn/s/blog_63e4cf2f0100tq4i.html 今天在POJ做了一道博弈题..进而了解到了阶梯博弈...下面阐述一下我对于阶梯博弈的理解. ...

  2. eclipse下使用tomcat启动maven项目

    最近学习使用maven,建立了一个maven项目使用eclipse下tomcat启动时报错: 严重: ContainerBase.addChild: start: org.apache.catalin ...

  3. SSH权威指南(转载)

    本书是一本介绍通信安全的书籍,如果你想保障你的通信安全,本书能给你一个很好的解决方案.本书从ssh协议介绍起,到具体的开源实现和商业实现.但本书同时介绍开源实现和商业实现,给人感觉比较乱.注意:由于o ...

  4. iOS7滑动返回

    [转载请注明出处] iOS 7中在传统的左上角返回键之外,提供了右滑返回上一级界面的手势.支持此手势的是UINavigationController中新增的属性 interactivePopGestu ...

  5. css3种方法实现元素的绝对居中

    元素的绝对居中应该是很多人熟悉的一个小应用,我记得很多年前去神州数码面试的时候就遇到过这个面试题.方法比较简单,代码如下: .node{ width : 300px; height : 400px; ...

  6. linux修改文件权限和用户组管理小结

    如何在linux下修改组权限 chmod g+r path/file 加读权限 当前目录 chmod -R g+r path/file 加读权限 当前目录以及子目录 g-r 减读权限g+w 加写权限g ...

  7. C#-datagridview设置列宽

    在使用datagridview的显示数据的过程中,常常会遇到需要设定datagridview的列宽,这就需要用到datagridview的属性: autosizemode

  8. .NET连接MySql数据库的方法及示例

    方法一: 使用MySQL推出的MySQL Connector/Net组件, 该组件是MySQL为ADO.NET访问MySQL数据库设计的.NET专用访问组件.完成该组件后,需要在项目中引用这个组件,也 ...

  9. [MODx] 10. Using Babel for Muti-languages support

    1. Go to 'Extras' -> download and install 'Babel'. 2. Set up '.htaccess' file, currently, we set ...

  10. Lazy Load 图片延迟加载(转)

    jQuery Lazy Load 图片延迟加载来源 基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. ...