JAXB - Annotations, Type Adapters: XmlJavaTypeAdapter
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的更多相关文章
- JAXB - Annotations, Type Mapping: XmlSchemaType
The annotation XmlSchemaType defines a mapping between an arbitrary Java type and a simple schema bu ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- JAXB - Annotations, Annotation for Classes: XmlType
This annotation adds information that would be available from a schema type, but isn't implied by a ...
- 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 ...
- 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 ...
随机推荐
- 转载StringBuilder说明
动态串StringBuilder 利用System.Text.StringBuilder类实现动态字符串,动态的含义是指在修改字符串时,系统不需要创建新的对象,不会重复开辟新的内存空间,而是直接在原来 ...
- SHH入门:Spring框架简介
(1)Spring 七大模块 核心容器:核心容器提供Spring 框架的基本功能.核心容器的主要组件是 BeanFactory,它是工厂模式的实现.BeanFactory 使用控制反转 (IOC) 模 ...
- Packetbeat协议扩展开发教程(2)
原文链接:http://elasticsearch.cn/article/53 书接上回:http://elasticsearch.cn/article/48 我们打开Packetbeat项目,看看里 ...
- 命令删除visualstudio.com云端项目(TFS)
1.运行命令行 2.tfsdeleteproject /collection:https://域名.visualstudio.com/DefaultCollection “项目名称”
- IntelliJ IDEA 使用教程 - AS3篇
喜欢IntelliJ IDEA的黑色皮肤,所以研究了下这个IDE的使用: 安装ActionScript Profiler插件: IDEA默认不带分析功能,需要下载安装该插件才行: File->S ...
- Egret的第一个2048游戏
http://bbs.egret-labs.org/thread-242-1-1.html 对于AS3的开发者来说,可以直接过渡到的HTML5框架就是Egret了,基本上是了解了TypeSprite的 ...
- POJ 3130 How I Mathematician Wonder What You Are! /POJ 3335 Rotating Scoreboard 初涉半平面交
题意:逆时针给出N个点,求这个多边形是否有核. 思路:半平面交求多边形是否有核.模板题. 定义: 多边形核:多边形的核可以只是一个点,一条直线,但大多数情况下是一个区域(如果是一个区域则必为 ).核内 ...
- 剑指OFFER之从二叉搜索树的后序遍历序列(九度OJ1367)
题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 输入: 每个测试案例包括2行: 第一行为1个整数 ...
- linux搜索jar内容
linux搜索 spring-beans-2.5.6.jar 内容 1.jar tvf spring-beans-2.5.6.jar -c 创建新的归档文件 -t 列出归档目录 -x 解压缩 ...
- 简单的谈一下.NET下的AOP
AOP有着它优良的好处, 从一个层面上去关心一个问题, .NET对此有良好的支持.如果硬生生的看MSDN不理解里面的思想是有掉入深渊的感觉的. 为了理解AOP的机制, 我们从需求开始说起,实现AOP就 ...