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. Win7旗舰版的nfs服务器如何架设? - Microsoft Community

    Win7旗舰版的nfs服务器如何架设? - Microsoft Community Win7旗舰版的nfs服务器如何架设?

  2. mysql 查询多个id

    select * from b1 where find_in_set('4',id); select * from b1 where id in (1,2,3,22);

  3. 【翻译】Android避免内存泄露(Activity的context 与Context.getApplicationContext)

    原谅地址:http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html ,英文原文在翻译之后 Android 应用 ...

  4. <转>HTML中的table转为excel

    转换html 中的table 为excel,firefox浏览器支持,代码如下 <%@ page language="java" contentType="text ...

  5. unigui多页签UI框架

    procedure TMainForm.openForm(Caption, FormClassName: string);var i: integer; sheet: TUniTabSheet;beg ...

  6. nginx+tomcat+redis负载均衡及session共享

    概述 本文档是用来详细描述 nginx+tomcat+redis负载均衡实现session共享 所需软件及下载地址 软件名称 下载地址 功能说明 Nginx-v1.6.0 http://nginx.o ...

  7. 有如下Student 对象,  private String name;       private int age;       private int score;   private String classNum;  其中,classNum

    package homework003; import java.util.ArrayList; import java.util.List; public class Text { public s ...

  8. CocoaPods使用命令

    5.需要在工程中创建一个Podfile文件, 使用命令: $cd /Users/shiyunlei/Desktop/CocoapodsSample(进入工程目录,cd后面的是工程的路径) $ touc ...

  9. 在WWDC 2014上,没提到的iOS 8 八大新特性

    今天凌晨1点,36氪如约为大家研磨出WWDC 2014全程 "贴身直播"(我不得不佩服牺牲个人时间,熬夜为大家奉上好文的5位氪星人:JasonZheng.WANGJINGYU.pa ...

  10. Android在MediaMuxer和MediaCodec用例 - audio+video

    在Android多媒体类,MediaMuxer和MediaCodec这是一个相对年轻,他们是JB 4.1和JB 4.3据介绍. 前者被用来产生一个混合的音频和视频的多媒体文件.的缺点是,现在可以只支持 ...