|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT |
javax.xml.bind.annotation Annotation Type XmlRootElement
@Target(value=TYPE)
将类或枚举类型映射到 XML 元素。
用法
@XmlRootElement 注释可以与以下程序元素一起使用:
- 顶层类
- 枚举类型
有关其他公共信息,请参阅 javax.xml.bind.package javadoc 中的“包规范”。
当使用 @XmlRootElement 注释对顶层类或枚举类型进行注释时,类型值被表示为 XML 文档中的 XML 元素。
此注释可与以下注释一起使用:
XmlType
、XmlEnum
、XmlAccessorType
、
XmlAccessorOrder
。
示例 1:将元素与 XML 模式类型关联
// Example: Code fragment @XmlRootElement class Point { int x; int y; Point(int _x,int _y) {x=_x;y=_y;} }
//Example: Code fragment corresponding to XML output marshal( new Point(3,5), System.out);
该注释会导致在模式中生成全局元素声明。全局元素声明与类映射到的 XML 模式类型关联。<!-- Example: XML output --> <point> <x> 3 </x> <y> 5 </y> </point>
<!-- Example:XML schema definition --> <xs:element name="point" type="point"> <xs:complextype name="point"> <xs:sequence> </xs:sequence></xs:complextype></xs:element><xs:element name="x" type="xs:int"> </xs:element><xs:element name="y" type="xs:int"> </xs:element>
示例 2:类型继承的正交性
某一类型上注释的元素声明不会被其派生类型继承。以下示例显示了这一点:
// Example: Code fragment @XmlRootElement class Point3D extends Point { int z; Point3D(int _x,int _y,int _z) {super(_x,_y);z=_z;} } //Example: Code fragment corresponding to XML output * marshal( new Point3D(3,5,0), System.out ); <!-- Example: XML output --> <!-- The element name is point3D not point --> <point3D> <x>3</x> <y>5</y> <z>0</z> </point3D> <!-- Example: XML schema definition --> <xs:element name="point3D" type="point3D"/> <xs:complexType name="point3D"> <xs:complexContent> <xs:extension base="point"> <xs:sequence> <xs:element name="z" type="xs:int"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType>示例 3:将全局元素与该类映射到的 XML 模式类型关联。
//Example: Code fragment @XmlRootElement(name="PriceElement") public class USPrice { @XmlElement public java.math.BigDecimal price; } <!-- Example: XML schema definition --> <xs:element name="PriceElement" type="USPrice"/> <xs:complexType name="USPrice"> <xs:sequence> <xs:element name="price" type="xs:decimal"/> </sequence> </xs:complexType>
since | JAXB2.0 |
Maps a class or an enum type to an XML element.
Usage
The @XmlRootElement annotation can be used with the following program elements:
- a top level class
- an enum type
See "Package Specification" in javax.xml.bind.package javadoc for additional common information.
When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document.
This annotation can be used with the following annotations:
XmlType
, XmlEnum
, XmlAccessorType
,
XmlAccessorOrder
.
Example 1: Associate an element with XML Schema type
// Example: Code fragment @XmlRootElement class Point { int x; int y; Point(int _x,int _y) {x=_x;y=_y;} }
//Example: Code fragment corresponding to XML output marshal( new Point(3,5), System.out);
The annotation causes an global element declaration to be produced in the schema. The global element declaration is associated with the XML schema type to which the class is mapped.<!-- Example: XML output --> <point> <x> 3 </x> <y> 5 </y> </point>
<!-- Example: XML schema definition --> <xs:element name="point" type="point"> <xs:complextype name="point"> <xs:sequence> </xs:sequence></xs:complextype></xs:element><xs:element name="x" type="xs:int"> </xs:element><xs:element name="y" type="xs:int"> </xs:element>
Example 2: Orthogonality to type inheritance
An element declaration annotated on a type is not inherited by its derived types. The following example shows this.
// Example: Code fragment @XmlRootElement class Point3D extends Point { int z; Point3D(int _x,int _y,int _z) {super(_x,_y);z=_z;} } //Example: Code fragment corresponding to XML output * marshal( new Point3D(3,5,0), System.out ); <!-- Example: XML output --> <!-- The element name is point3D not point --> <point3D> <x>3</x> <y>5</y> <z>0</z> </point3D> <!-- Example: XML schema definition --> <xs:element name="point3D" type="point3D"/> <xs:complexType name="point3D"> <xs:complexContent> <xs:extension base="point"> <xs:sequence> <xs:element name="z" type="xs:int"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType>Example 3: Associate a global element with XML Schema type to which the class is mapped.
//Example: Code fragment @XmlRootElement(name="PriceElement") public class USPrice { @XmlElement public java.math.BigDecimal price; } <!-- Example: XML schema definition --> <xs:element name="PriceElement" type="USPrice"/> <xs:complexType name="USPrice"> <xs:sequence> <xs:element name="price" type="xs:decimal"/> </sequence> </xs:complexType>
- Since:
- JAXB2.0
- Author:
- Sekhar Vajjhala, Sun Microsystems, Inc.
Optional Element Summary | |
---|---|
String |
name
local name of the XML element. |
String |
namespace
namespace name of the XML element. |
abstract public String
namespace()
XML 元素的名称空间名。
如果该值为 "##default",那么 XML 名称空间名派生于类 (XmlSchema
) 的包。如果没有对包命名,那么 XML 名称空间是默认的空名称空间。
namespace
public abstract String namespace
- namespace name of the XML element.
If the value is "##default", then the XML namespace name is derived from the package of the class (
XmlSchema
). If the package is unnamed, then the XML namespace is the default empty namespace. - Default:
- "##default"
abstract public String
name()
XML 元素的本地名称。
如果值为 "##default",则名称派生于类名称。
name
public abstract String name
- local name of the XML element.
If the value is "##default", then the name is derived from the class name.
- Default:
- "##default"
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT |
Submit a bug or feature
Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.
PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!