A class that describes an XML element that is to be a top-level element, i.e., one that can function as an XML document, should be annotated with XmlRootElement. Its optional elements are name and namespace. By default, the class name is used as the name.

This annotation corresponds to an xsd:element construct being used at the outermost level of an XML schema. The sequence of Java, XML and schema snippets given below illustrates this relation.

@XmlRootElement( name="doc" )
public class Document {
@XmlElement
protected Foo foo;
// ...
}
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<foo>...</foo>
</doc>
<?xml version="1.0" encoding="UTF-8"?>
<xsd:complexType name="Foo">
...
</xsd:complexType>
<xsd:complexType name="Document">
<xsd:sequence>
<xsd:element name="foo" type="Foo"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="doc" type="Document"/>

It's a surprising fact that if all of your Java classes permit a straightforward mapping to XML Schema, XmlRootElement may be the only annotation you have to make! Here's a small set of classes, that is even capable of marshalling a Map<K,V>.

import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.*; @XmlRootElement(name="doc")
public class DocType {
public Map<KeyType,EntryType> key2entry =
new HashMap<KeyType,EntryType>();
public DocType(){
}
} import javax.xml.datatype.*;
public class KeyType {
public String event;
public XMLGregorianCalendar datetime;
public KeyType(){}
public KeyType( String event, XMLGregorianCalendar datetime ){
this.event = event;
this.datetime = datetime;
}
} public class EntryType {
public String program;
public String artists; public EntryType(){}
public EntryType( String artists, String program ){
this.artists = artists;
this.program = program;
}
}

Applying the usual incantations for creating and marshalling content, you could produce XML data like so:

<doc>
<key2entry>
<entry>
<key>
<event>Soiree</event>
<datetime>2008-08-23T20:00:00</datetime>
</key>
<value>
<program>Man on the Moon</program>
<artists>R.E.M</artists>
</value>
</entry>
</key2entry>
</doc>

The XMLGregorianCalendar is mapped to xsd:dateTime, and the 'T' between the date and the time is just right, according to the Schema Datatypes specification. You can see that JAXB had to "invent" a few tag names for the intermediary element levels separating map entries from each other, and key data from value data, but you'd have to do something similar if you'd design it yourself.

JAXB - Annotations, Top-level Elements: XmlRootElement的更多相关文章

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

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

  3. PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)

    1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...

  4. get top k elements of the same key in hive

    key points: 1. group by key and sort by using distribute by and sort by. 2. get top k elements by a ...

  5. invalid nib registered for identifier (重用符) - nib must contain exactly one top level object which must be a UITableViewCell instance'

    通过xib创建cell的时候 一定要注意!!! 这个错误是在这个xib中在View同一层级出现了其他的控件,检查一下xib中左边的层级关系,让cell的view是唯一的控件就可以了,否则一执行 就会提 ...

  6. 什么时候需要交换Top Level ?

    什么时候需要交换Top Level ? 上一篇中提到,如果采用仿真的时候,运用门级仿真就需要进行顶层交换,RTL仿真不需要,那么什么时候需要呢? QuartusII 向下包含,在Project Nav ...

  7. 编译openwrt时报错:fstools-2018-01-02-11efbf3b/libfstools/overlay.c: At top level: cc1: error: unrecognized command line option '-Wno-format-truncation' [-Werror]

    1. 详细错误信息 [ 11%] Building C object CMakeFiles/fstools.dir/libfstools/overlay.c.o/home/jello/openwrt/ ...

  8. top level element is not completed

    今天在使用IDEA配置springmvc文件时,出现类似在Android studio 中样式文件报top level element is not completed错,郁闷极了,找了好久 才找到解 ...

  9. 解决Cannot find config.m4 Make sure that you run '/home/php/bin/phpize' in the top level source directory of the module

    oot@DK:/home/daokr/downfile/php-7.0.0/ext/mysqlnd# /home/php/bin/phpizeCannot find config.m4. Make s ...

随机推荐

  1. javascript设计模式8

    桥接模式(将抽象与其实现隔离开来,以便二者独立变化) function sendInfo(element){ var id=element.id; ajax("GET"," ...

  2. 题目1043:Day of Week(输入日期与当前日起天数差%7,在做相关星期调整)

    题目描述: We now use the Gregorian style of dating in Russia. The leap years are years with number divis ...

  3. Tasks on 2013

    1.改进并补充实验 2.样本选取和文档分布调查 3. Diversity Metrics 4. PPT for Project starting 1. Chrome & webkit 2. O ...

  4. [思维题]Bored Qishen

    给出一个整数集,其中包含1-n的所有整数,要求挑选出一个元素最多的子集,使得子集中任意两数的乘积不是完全平方数 (n<=10^6) 求这样一个最大子集的元素个数 #include <cst ...

  5. 转载 8天掌握EF的Code First开发之Entity Framework介绍

    转载原地址:  http://www.cnblogs.com/farb/p/IntroductionToEF.html Entity Framework概要 Entity Framework是微软的O ...

  6. docker——Dockerfile创建镜像

    写在前面: 继续docker的学习,昨天用docker成功跑了tomcat,但是在centos中镜像跑的容器手动装jdk和tomcat,今天学习用Dockerfile创建镜像,并在上面搭建java环境 ...

  7. java 实现视频转换通用工具类:视频截图-Ffmpeg(四)

    java 实现视频转换通用工具类:获取视频元数据信息(一) java 实现视频转换通用工具类:视频相互转换-总方法及Mencoder(二) java 实现视频转换通用工具类:视频相互转换-Ffmpeg ...

  8. cc2530 timer 1 PWM 输出

    需要在ZStack 协议栈里使用PWM,于是使用其16bit的timer 1来实现之.使用 P1_0口输出,使用的是正计数/倒计数模式,占空比为50%.代码如下: #include <ioCC2 ...

  9. 感动前行——给医学媳妇写的演讲稿(非IT类)

    感动前行 我是一个平庸的人,走在人群中大家可能不能辨别出我.我是一个平庸的人,每天上班.工作.吃饭.睡觉.我是一个平庸的人,来了医院多半年也仅仅和检验科的同事相对照较熟悉,其它科室人员非常少有交流. ...

  10. IPv6介绍

    一.为什么需要IPv6 为了扩大地址空间,拟通过IPv6重新定义地址空间.IPv4采用32位地址长度,只有大约43亿个地址,估计在2005-2010年间将被分配完毕,而IPv6采用128位地址长度,几 ...