@RetentionSE(valueSE=RUNTIMESE) @TargetSE(valueSE=TYPESE) public @interface XmlRootElement
使用方法
@XmlRootElement アノテーションは、次のプログラム要素で使用できます。
追加の共通情報については、jakarta.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);
<!-- Example: XML output -->
<point>
<x> 3 </x>
<y> 5 </y>
</point>
アノテーションにより、グローバル要素宣言がスキーマに生成されます。グローバル要素宣言は、クラスがマップされている XML スキーマ型に関連付けられています。
<!-- 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>
Copyright © 2018,2020 Eclipse Foundation.
Use is subject to license terms.