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 ...
随机推荐
- 关于java线程池 Ⅱ
上一篇翻译了线程池主要部分的api,经过一段时间的学习,这里记录一下这段时间对jdk自带线程池的学习成果. 为了方便说明,先放一张类图,包括了jdk线程池主要涉及到的类,为了条理清晰去掉了部分依赖和关 ...
- BNUOJ-26580 Software Bugs KMP匹配,维护
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26580 题意:给一个模式串,然后m个匹配串,要求删掉匹配串中的所有存在的模式串,使得余下的 ...
- TCP/IP协议详解内容总结
TCP/IP协议 TCP/IP不是一个协议,而是一个协议族的统称.里面包括IP协议.IMCP协议.TCP协议. TCP/IP分层: 这里有几个需要注意的知识点: 互联网地址:也就是IP地址,一般为 ...
- Camtasia Studio的安装步骤
总的来说: 1.安装 2.安装之后,进行汉化. 破解方法: 1):输入注册码安装 用户名:6-Y 注册码:GCABC-CPCCE-BPMMB-XAJXP-S8F6R 2):软件汉化 安装完成后使用汉化 ...
- A Tour of Go Methods and Interfaces
The next group of slides covers methods and interfaces, the constructs that define objects and their ...
- Java编程 -- 命名规范
转自:http://www.hawstein.com/posts/google-java-style.html#Naming 命名约定 5.1 对所有标识符都通用的规则 标识符只能使用ASCII字母和 ...
- NPOI导出Excel示例
摘要:使用开源程序NPOI导出Excel示例.NPOI首页地址:http://npoi.codeplex.com/,NPOI示例博客:http://tonyqus.sinaapp.com/. 示例编写 ...
- C#-datagridview右键选中行
在datagridview中有时需要在右键点击某行的时候就选中它,那么我们只需要在datagridview的CellMonseDown事件中添加如下代码就行: && e.ColumnI ...
- HOG特征-理解篇
网上去找关于HOG的资料,发现理解性的较少,并且较为冗长,为方便大家理解便自己写了篇,希望能对奋斗在特征提取第一线的同志们有所帮助: HOG即histogram of oriented gradien ...
- 7Zip 来备份重要文件(夹)
body { font-family: 宋体,Georgia,Helvetica,Arial,sans-serif,宋体,serif; font-size: 10.5pt; line-height: ...