|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 必需 | 可选 | 详细信息: 元素 | |||||||||
@Retention(value=RUNTIME) @Target(value=TYPE) public @interface XmlRootElement
将类或枚举类型映射到 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:element name="x" type="xs:int"/> <xs:element name="y" type="xs:int"/> </xs:sequence> </xs:complexType>
示例 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>
| 可选元素摘要 | |
|---|---|
String |
name XML 元素的本地名称。 |
String |
namespace XML 元素的名称空间名。 |
public abstract String namespace
如果该值为 "##default",那么 XML 名称空间名派生于类 (XmlSchema) 的包。如果没有对包命名,那么 XML 名称空间是默认的空名称空间。
public abstract String name
如果值为 "##default",则名称派生于类名称。
|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 必需 | 可选 | 详细信息: 元素 | |||||||||
版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。