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. SQL Server优化相关的工具脚本

    SQL Server性能优化的一些常用脚本,适用于SQL Server 2008,更高的版本某些系统表的字段有所不同,建议参考MSDN. 死锁相关 /************************* ...

  2. Delphi外挂开发网站

    http://cheatengine.org/http://wenku.baidu.com/view/2d5de818964bcf84b9d57b15.html   [delphi外G]http:// ...

  3. 【C语言】-循环结构-for语句

    for语句: for (表达式1;表达式2;表达式3) { 循环语句 } 表达式1 给循环变量赋初值 表达式2 为循环条件 表达式3 用来修改循环变量的值,称为循环步长. for语句的执行流程: 例: ...

  4. [Objective-c 基础 - 3.2] ARC

    A.ARC的基本原理 1.ARC的判断原则:只有没有强指针指向对象,就会立即释放对象 注意,在以下情况,会立即回收内存: (1)指针超出作用域 { Person *p1 = [[Personalloc ...

  5. 转载 SharePoint 2013配置Master Page and Page Layout

    转载原地址: http://www.cnblogs.com/huangjianwu/p/4539706.html 涉及到的内容是关于SharePoint 2013如何部署自定义的母版页和布局页. 进入 ...

  6. box-flex 分割

    <!DOCTYPE html> <html> <head> <style> .box{ width: 300px; height: 100px; bac ...

  7. sublime Text 3的默认快捷键大全

    Ctrl+M 光标跳至对应的括号 Alt+. 闭合当前标签 Ctrl+Shift+A 选择光标位置父标签对儿 Ctrl+Shift+[ 折叠代码 Ctrl+Shift+] 展开代码 Ctrl+KT 折 ...

  8. 1098. Insertion or Heap Sort (25)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  9. MVC上传文件目录至共享目录

    1.需在共享目录的服务器上加入一个有权限(所有权限,包括读.写.删除等权限)的账号名2.MVC站点webconfig文件中,<system.web>节点中加入配置节点,    <id ...

  10. 【数据结构】非常有用的hash表

        这篇博客的目的是让尚未学会hash表的朋友们对hash表有一个直观的理解,并且能根据本文定义出属于自己的第一个hash表,但算不上研究文,没有深究概念和成功案例.         什么是has ...