xml @XMLRootElement 与 @XmlType
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11520724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@XMLRootElement versus @XmlType
提问by CodeClimber
What's the difference between annotating a class with @XMLRootElementand @XMLType. I've been annotating classes with @XMLTypewhen the structure will be used more than once within an XML schema and with @XMLRootElementwhen it will be used only once - is this the best approach?
用@XMLRootElement和注释一个类有什么区别@XMLType。我一直在注释类,@XMLType何时在 XML 模式中多次使用结构,@XMLRootElement何时只使用一次 - 这是最好的方法吗?
A different but related question which I'll include here. The @XMLTypeannotation has an propOrder attribute to specify in which order its elements appear - is there an equivalent for @XMLRootElement?
一个不同但相关的问题,我将在这里包括。该@XMLType标注有一个propOrder属性来指定其中责令其元素出现-是有一个相当于为@XMLRootElement?
I'm using these annotations in conjunction with JAX-WS annotations to create web services if that makes any difference.
如果有任何区别,我将这些注释与 JAX-WS 注释结合使用来创建 Web 服务。
采纳答案by DocWatson
The difference between XmlRootElementand XmlTypeis a matter of scoping. Remember this annotation is merely dictating the creation of the schema used to generate your XML. The XmlRootElementdenotes a global element (with an anonymous or schema type):
XmlRootElement和之间的区别XmlType是范围界定的问题。请记住,此注释仅指示用于生成 XML 的模式的创建。的XmlRootElement表示一个全局元素(与匿名或模式类型):
<xs:element name=foo type="bar"> </xs:element> <-- schema type
while the XmlTypeis used to denote a local element (with an anonymous or complex type):
而XmlType用于表示本地元素(具有匿名或复杂类型):
<xs:complexType name=bar> </xs:complexType> <-- complex type
The main differences in local/global here are in the hierarchy of the schema your object will appear in and whether you are declaring a schema type or complex type. The documentation for both of these annotations is well written and includes examples:
此处本地/全局的主要区别在于您的对象将出现在其中的架构的层次结构以及您是声明架构类型还是复杂类型。这两个注释的文档写得很好,包括示例:
EDIT: Addressing the propOrderquestion: you can use it on a global element if you are also declaring a local type:
编辑:解决propOrder问题:如果您还声明本地类型,则可以在全局元素上使用它:
@XmlRootElement (name="PersonElement")
@XmlType (propOrder={"firstname", "lastname"})
public class People{
@XmlElement
public String firstname;
public String lastname;
}
This will yield something like:
这将产生类似于:
<xs:element name="PersonElement" type="People"/>
<xs:complexType name="People">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
回答by bdoughan
I've been annotating classes with @XMLType when the structure will be used more than once within an XML schema and with @XMLRootElement when it will be used only once - is this the best approach?
当结构将在 XML 模式中多次使用时,我一直使用 @XMLType 注释类,而当它只使用一次时使用 @XMLRootElement 注释类 - 这是最好的方法吗?
One thing to know is that neither the @XmlRootElementor @XmlTypeannotation is required. They aren't the equivalent of @Entityfrom JPA. You can use a JAXB (JSR-222)implementation without any annotations what so ever:
需要知道的一件事是, the@XmlRootElement或@XmlTypeannotation都不是必需的。它们不等同于@Entity来自 JPA。您可以使用JAXB (JSR-222)实现而无需任何注释:
Below I'll explain what @XmlRootElementand @XmlTypedo.
下面我将解释什么@XmlRootElement和@XmlType做什么。
@XmlRootElement
@XmlRootElement
There are times when your JAXB implementation needs to instantiate an object based only on the XML element that is being processed. The @XmlRootElementannotation is the primary means of specifying this association. Note if a class corresponds to more than one XML element then the @XmlElementDeclannotation should be used insteat,
有时,您的 JAXB 实现需要仅基于正在处理的 XML 元素来实例化对象。的@XmlRootElement注释是指定此关联的主要手段。请注意,如果一个类对应于多个 XML 元素,@XmlElementDecl则应使用注释代替,
ROLE #1 - Specifying the Root Object
角色 #1 - 指定根对象
@XmlRootElementis primarily used to specify the root object. This is so when your JAXB implementation begins unmarshalling an XML document it knows what object to instantiate. Almost all subsequent annotations will be based on information gathered from the parent class.
@XmlRootElement主要用于指定根对象。这就是当您的 JAXB 实现开始解组 XML 文档时,它知道要实例化的对象。几乎所有后续注释都将基于从父类收集的信息。
Foo
富
@XmlRootElement(name="root")
public class Foo {
private String name;
}
Bar
酒吧
public class Bar {
private String name;
}
XML
XML
<root>
<name>Jane Doe</name>
</root>
Demo
演示
Foo foo = (Foo) unmarshaller.unmarshal(xml);
Bar bar = unmarshaller.unmarshal(xml, Bar.class).getValue();
ROLE #2 - Substitution Groups
角色#2 - 替换组
The @XmlElementRefannotation delegates the type of object instantiated to the name/uri of the element. This enables the mapping to the concept of substitution groups for representing inheritance.
该@XmlElementRef注释的代表对象的实例化名称的类型/元素的URI。这使得映射到用于表示继承的替代组的概念成为可能。
ROLE #3 - Any Content
角色 #3 - 任何内容
@XmlAnyElementallows you to map a wild card section of your XML document. If you specify @XmlAnyElement(lax=true)then elements associated with domain objects will be converted to the corresponding domain object.
@XmlAnyElement允许您映射 XML 文档的通配符部分。如果您指定,@XmlAnyElement(lax=true)则与域对象关联的元素将转换为相应的域对象。
@XmlType
@XmlType
ROLE #1 - Schema Gen
角色 #1 - 模式生成
By default a named complex type is generated for each Java class known to the JAXB context. You can control the name of this type using the @XmlTypeannotation, or specify that an anonymous complex type should be generated by specifying the name as "".
默认情况下,会为 JAXB 上下文已知的每个 Java 类生成一个命名的复杂类型。您可以使用@XmlType注释来控制此类型的名称,或者通过将名称指定为 来指定应生成匿名复杂类型""。
ROLE #2 - Inheritance and xsi:type
角色 #2 - 继承和 xsi:type
By default JAXB leverages the xsi:typeattribute as the inheritance indicator. The value on this attribute corresponds to the name and namespace you have specified on the @XmlTypeannotation, or is defaulted based on the class.
默认情况下,JAXB 利用该xsi:type属性作为继承指示符。此属性上的值对应于您在@XmlType注释上指定的名称和命名空间,或者基于类默认。
ROLE #3 - Prop Order
角色 #3 - 道具订单
As you mention you can use the @XmlTypeto specify the property order.
正如您所提到的,您可以使用@XmlType来指定属性顺序。
ROLE #4 - Factory Methods
角色 #4 - 工厂方法
@XmlTypeallows you to specify a factory class and/or method that can be used to instantiate the domain object instead of the default constructor.
@XmlType允许您指定可用于实例化域对象而不是默认构造函数的工厂类和/或方法。
A different but related question which I'll include here. The @XMLType annotation has an propOrder attribute to specify in which order it's elements appear - is there an equivalent for @XMLRootElement?
一个不同但相关的问题,我将在这里包括。@XMLType 注释有一个 propOrder 属性来指定它的元素出现的顺序 - @XMLRootElement 是否有等价物?
No, the propOrderaspect belongs to the @XmlTypeannotation. This makes sense since complex types are responsible for specifying an (or lack of) order. You can of course use these annotations at the same time.
不,propOrder方面属于@XmlType注释。这是有道理的,因为复杂类型负责指定(或缺少)顺序。您当然可以同时使用这些注释。
@XmlRootElement
@XmlType(propOrder={"foo", "bar"}
public class Root {
...
}

