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. 安森美电量计采用内部电阻跟踪电流--电压HG-CVR

    http://www.dianyuan.com/article/34608.html LC709203F应用:用于便携式设备单节锂电池的智能电量计http://files.cnblogs.com/fi ...

  2. PCb过孔大小设置 / 丝印层字符尺寸设置

    PCb过孔大小一般设置为:内孔(孔尺寸)0.30(12mil),外壳(直径)0.6(24mil) 常用过孔设置: 内径: 15mil(0.381mm)  30mil(0.762mm) 外径: 20mi ...

  3. [OC Foundation框架 - 9] NSMutableArray

    可变的NSArray,可以随意添加OC对象   1.创建 void arrayCreate() { NSMutableArray *array = [NSMutableArray arrayWithO ...

  4. [一]JQueryMobile简介

    JQueryMobile 基于JQuery,实现对不同尺寸手机屏幕的支持,提供了许多组件,以及对于手机端的常用事件(touch.tap.taphold) 如何使用 1.引入jquery.js.jque ...

  5. Partition Array

    Given an array nums of integers and an int k, partition the array (i.e move the elements in "nu ...

  6. 避免SWF被内存提取工具提取的方法

    内存工具从内存中抓取SWF一般是依靠寻找SWF的前7个字节(3个SWF文件必有的标示字节“FWS”或“CWS”或“ZWS”+4个记录该SWF文件长度的字节),所以避免被提取我们只要在加载SWF到内存后 ...

  7. ECSHOP在线手册布局参考图--登录/注册页 user_passport.dwt

        A.会员登录框 1,设置方法 自带模块 2,代码相关 user_passport.dwt 中 <div class="usBox_1 f_l"> <div ...

  8. CAS Proxy 的相关文章

    cas代理模式的原理及配置 http://my.oschina.net/mashiguang/blog/69312 剖析CAS Proxy的设计原理 http://www.blogjava.net/s ...

  9. Android真机抓屏- Android Screen Monitor

    一般运行Android应用程序有两种方式一种是设置Android虚拟设备模拟器,通过Android  Virtual Manger进行管理,一种是插入USB数据线直接真机上进行调试,但是如果电脑配置比 ...

  10. Nape的回调系统 nape.callbacks

    在Nape中增加一个回调大致分为三步 1:定义一些标签,并根据需求为不同的Interactor打上不同的标签 2:定义一个监听器,这个监听器定义了哪些标签触发了哪种行为之后做何种回调 3:为Space ...