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

时间: 2024-08-04 19:11:45

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

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 extensio

top level element is not completed

今天在使用IDEA配置springmvc文件时,出现类似在Android studio 中样式文件报top level element is not completed错,郁闷极了,找了好久 才找到解决方案,问题解决了  然而并没有理解为什么 ,希望有大神可以帮回复.问题类似如下: <resources> <!-- Base application theme. --> <style name="AppTheme" parent=""T

invalid nib registered for identifier (重用符) - nib must contain exactly one top level object which must be a UITableViewCell instance&#39;

通过xib创建cell的时候 一定要注意!!! 这个错误是在这个xib中在View同一层级出现了其他的控件,检查一下xib中左边的层级关系,让cell的view是唯一的控件就可以了,否则一执行 就会提示 "* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (Cell) - nib must co

什么时候需要交换Top Level ?

什么时候需要交换Top Level ? 上一篇中提到,如果采用仿真的时候,运用门级仿真就需要进行顶层交换,RTL仿真不需要,那么什么时候需要呢? QuartusII 向下包含,在Project Navigator中可以查看到整个工程包含的module,在牵涉到硬件电路上的相关配置时,QuartusII 只会从这个Top Level中向下包含,所以在门级仿真的时候就必须交换到当前的顶层.在Top Level之外的module是不会被使用的. 但是在Files选项卡中会看到工程文件夹中所有的设计输入

Protobuf的奇怪问题解决--Expected top - level statement (e.g &quot;message&quot;)

最近天天都在玩Protobuf,因此,最近总结的问题过半都是关于它的.话说昨天遇到一个很奇怪的问题,转换.proto为java文件的过程中,报错:Expected top - level statement (e.g "message").截图如下: 查询问题原因,原来是由于编码格式的问题,因为,我使用UE打开了文件,并保存为UTF-8的形式.因此,会在文件的开头有一个特殊的字符,详见下图: 通过查找发现了问题的原因,由于我使用的是中文简体的windows,因此,他的默认编码为GB23

Expressions are not allowed at the top level

Swift中,直接在类的外面调用类内部的方法,会出现Bug:Expressions are not allowed at the top level. 原因是: 在App工程里, .swift 文件都是编译成模块的,不能有  top level code. 先明确一个概念,一个 .swift 文件执行是从它的第一条非声明语句(表达式.控制结构)开始的,同时包括声明中的赋值部分(对应为 mov 指令或者 lea 指令),所有这些语句,构成了该 .swift 文件的 top_level_code()

react 中使用import()实现按需加载报错 解决方法 --‘import’ and ‘export’ may only appear at the top level

因为项目需要搞一下按需加载,使用import实现代码打包分片按需加载,但是在实际使用中报语法错误.报错信息如下 SyntaxError: 'import' and 'export' may only appear at the top level 啊咧?报错了. 查找发现很多人碰到过,解决方法不同,但是我这个报错适用下边这个方法. npm install --save-dev babel-plugin-syntax-dynamic-import 然后调整babel-loader配置如下: use

JAXB - Annotations, Annotation for Classes: XmlType

This annotation adds information that would be available from a schema type, but isn't implied by a Java class declaration. The annotation has several attributes: factoryClass and factoryMethod define the class containing a no-argument method for cre

get top level domain by php

function rLastParse($str, $sep, $cnt = 1, $complete_if_not = false){ $i = strrpos($str, $sep); while (($cnt > 1) && ($i !== false)) { $i = strrpos(substr($str, 0, $i), $sep); $cnt--; } if ($i === false) { return $complete_if_not ? $str : ''; }