@XMLRootElement与@XmlType [英] @XMLRootElement versus @XmlType
问题描述
使用 @XMLRootElement
和 @XMLType
注释类之间的区别是什么。当结构将在XML模式中多次使用并且 @XMLRootElement $ c $时,我一直用
@XMLType
注释类。 c>何时只使用一次 - 这是最好的方法吗?
What's the difference between annotating a class with @XMLRootElement
and @XMLType
. 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?
我将在此处包含一个不同但相关的问题。 @XMLType
注释有一个propOrder属性,用于指定其元素出现的顺序 - @XMLRootElement
的等价物?
A different but related question which I'll include here. The @XMLType
annotation has an propOrder attribute to specify in which order its elements appear - is there an equivalent for @XMLRootElement
?
我正在使用这些注释与JAX-WS注释一起创建Web服务,如果这有任何区别的话。
I'm using these annotations in conjunction with JAX-WS annotations to create web services if that makes any difference.
推荐答案
XmlRootElement
和 XmlType
之间的区别是作用域。请记住,此注释仅指示用于生成XML的模式的创建。 XmlRootElement
表示全局元素(具有匿名或模式类型):
The difference between XmlRootElement
and XmlType
is a matter of scoping. Remember this annotation is merely dictating the creation of the schema used to generate your XML. The XmlRootElement
denotes a global element (with an anonymous or schema type):
<xs:element name=foo type="bar"> </xs:element> <-- schema type
而 XmlType
用于表示本地元素(具有匿名或复杂类型):
while the XmlType
is used to denote a local element (with an anonymous or complex type):
<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:
编辑:解决 propOrder
问题:你可以使用如果你也声明一个本地类型,它在全局元素上:
Addressing the propOrder
question: you can use it on a global element if you are also declaring a local type:
@XmlRootElement (name="PersonElement")
@XmlType (propOrder={"firstname", "lastname"})
public class People{
@XmlElement
public String firstname;
public String lastname;
}
这将产生如下结果:
<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>
这篇关于@XMLRootElement与@XmlType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!