1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/* * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.xml.bind.annotation; import java.lang.annotation.Retention; import java.lang.annotation.Target; import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.ElementType.TYPE; /** * Maps a class or an enum type to an XML element. * * <p> <b>Usage</b> </p> * <p> * The @XmlRootElement annotation can be used with the following program * elements: * <ul> * <li> a top level class </li> * <li> an enum type </li> * </ul> * * <p>See "Package Specification" in javax.xml.bind.package javadoc for * additional common information.</p> * * <p> * 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. * * <p> This annotation can be used with the following annotations: * {@link XmlType}, {@link XmlEnum}, {@link XmlAccessorType}, * {@link XmlAccessorOrder}. * <p> * <p> * <b>Example 1: </b> Associate an element with XML Schema type * <pre> * // Example: Code fragment * @XmlRootElement * class Point { * int x; * int y; * Point(int _x,int _y) {x=_x;y=_y;} * } * </pre> * * <pre> * //Example: Code fragment corresponding to XML output * marshal( new Point(3,5), System.out); * </pre> * * <pre><xmp> * <!-- Example: XML output --> * <point> * <x> 3 </x> * <y> 5 </y> * </point> * </xmp></pre> * * 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. * * <pre><xmp> * <!-- 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> * </xmp></pre> * * <p> * * <b>Example 2: Orthogonality to type inheritance </b> * * <p> * An element declaration annotated on a type is not inherited by its * derived types. The following example shows this. * <pre> * // 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> * </pre> * * <b>Example 3: </b> Associate a global element with XML Schema type * to which the class is mapped. * <pre> * //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> * </pre> * * @author Sekhar Vajjhala, Sun Microsystems, Inc. * @since JAXB2.0 */ @Retention(RUNTIME) @Target({TYPE}) public @interface XmlRootElement { /** * namespace name of the XML element. * <p> * If the value is "##default", then the XML namespace name is derived * from the package of the class ( {@link XmlSchema} ). If the * package is unnamed, then the XML namespace is the default empty * namespace. */ String namespace() default "##default"; /** * local name of the XML element. * <p> * If the value is "##default", then the name is derived from the * class name. * */ String name() default "##default"; }