| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| <!-- NewPage --> |
| <html lang="en"> |
| <head> |
| <title>XmlElementRef</title> |
| <link rel="stylesheet" type="text/css" href="../../../../stylesheet.css" title="Style"> |
| <script type="text/javascript" src="../../../../script.js"></script> |
| |
| <link rel="shortcut icon" href="/img/jakarta-favicon.ico"> |
| </head> |
| <body> |
| <script type="text/javascript"><!-- |
| try { |
| if (location.href.indexOf('is-external=true') == -1) { |
| parent.document.title="XmlElementRef"; |
| } |
| } |
| catch(err) { |
| } |
| //--> |
| </script> |
| <noscript> |
| <div>JavaScript is disabled on your browser.</div> |
| </noscript> |
| <!-- ========= START OF TOP NAVBAR ======= --> |
| <div class="topNav"><a name="navbar.top"> |
| <!-- --> |
| </a> |
| <div class="skipNav"><a href="#skip.navbar.top" title="Skip navigation links">Skip navigation links</a></div> |
| <a name="navbar.top.firstrow"> |
| <!-- --> |
| </a> |
| <ul class="navList" title="Navigation"> |
| <li><a href="../../../../overview-summary.html">Overview</a></li> |
| <li><a href="package-summary.html">Package</a></li> |
| <li class="navBarCell1Rev">Class</li> |
| <li><a href="package-tree.html">Tree</a></li> |
| <li><a href="../../../../deprecated-list.html">Deprecated</a></li> |
| <li><a href="../../../../index-all.html">Index</a></li> |
| <li><a href="../../../../help-doc.html">Help</a></li> |
| </ul> |
| </div> |
| <div class="subNav"> |
| <ul class="navList"> |
| <li><a href="../../../../jakarta/xml/bind/annotation/XmlElementDecl.GLOBAL.html" title="class in jakarta.xml.bind.annotation"><span class="typeNameLink">Prev Class</span></a></li> |
| <li><a href="../../../../jakarta/xml/bind/annotation/XmlElementRef.DEFAULT.html" title="class in jakarta.xml.bind.annotation"><span class="typeNameLink">Next Class</span></a></li> |
| </ul> |
| <ul class="navList"> |
| <li><a href="../../../../index.html?jakarta/xml/bind/annotation/XmlElementRef.html" target="_top">Frames</a></li> |
| <li><a href="XmlElementRef.html" target="_top">No Frames</a></li> |
| </ul> |
| <ul class="navList" id="allclasses_navbar_top"> |
| <li><a href="../../../../allclasses-noframe.html">All Classes</a></li> |
| </ul> |
| <div> |
| <script type="text/javascript"><!-- |
| allClassesLink = document.getElementById("allclasses_navbar_top"); |
| if(window==top) { |
| allClassesLink.style.display = "block"; |
| } |
| else { |
| allClassesLink.style.display = "none"; |
| } |
| //--> |
| </script> |
| </div> |
| <div> |
| <ul class="subNavList"> |
| <li>Summary: </li> |
| <li>Field | </li> |
| <li>Required | </li> |
| <li><a href="#annotation.type.optional.element.summary">Optional</a></li> |
| </ul> |
| <ul class="subNavList"> |
| <li>Detail: </li> |
| <li>Field | </li> |
| <li><a href="#annotation.type.element.detail">Element</a></li> |
| </ul> |
| </div> |
| <a name="skip.navbar.top"> |
| <!-- --> |
| </a></div> |
| <!-- ========= END OF TOP NAVBAR ========= --> |
| <!-- ======== START OF CLASS DATA ======== --> |
| <div class="header"> |
| <div class="subTitle">jakarta.xml.bind.annotation</div> |
| <h2 title="Annotation Type XmlElementRef" class="title">Annotation Type XmlElementRef</h2> |
| </div> |
| <div class="contentContainer"> |
| <div class="description"> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <hr> |
| <br> |
| <pre>@Retention(value=RUNTIME) |
| @Target(value={FIELD,METHOD}) |
| public @interface <span class="memberNameLabel">XmlElementRef</span></pre> |
| <div class="block"><p> |
| Maps a JavaBean property to a XML element derived from property's type. |
| <p> |
| <b>Usage</b> |
| <p> |
| <code>@XmlElementRef</code> annotation can be used with a |
| JavaBean property or from within <a href="../../../../jakarta/xml/bind/annotation/XmlElementRefs.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlElementRefs</code></a> |
| <p> |
| This annotation dynamically associates an XML element name with the JavaBean |
| property. When a JavaBean property is annotated with <a href="../../../../jakarta/xml/bind/annotation/XmlElement.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlElement</code></a>, the XML element name is statically derived from the |
| JavaBean property name. However, when this annotation is used, the |
| XML element name is derived from the instance of the type of the |
| JavaBean property at runtime. |
| |
| <h2> XML Schema substitution group support </h2> |
| XML Schema allows a XML document author to use XML element names |
| that were not statically specified in the content model of a |
| schema using substitution groups. Schema derived code provides |
| support for substitution groups using an <i>element property</i>, |
| (section 5.5.5, "Element Property" of Jakarta XML Binding specification). An |
| element property method signature is of the form: |
| <pre><code> |
| public void setTerm(JAXBElement<? extends Operator>); |
| public JAXBElement<? extends Operator> getTerm(); |
| </code></pre> |
| <p> |
| An element factory method annotated with <a href="../../../../jakarta/xml/bind/annotation/XmlElementDecl.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlElementDecl</code></a> is |
| used to create a <code>JAXBElement</code> instance, containing an XML |
| element name. The presence of <code>@XmlElementRef</code> annotation on an |
| element property indicates that the element name from <code>JAXBElement</code> |
| instance be used instead of deriving an XML element name from the |
| JavaBean property name. |
| |
| <p> |
| The usage is subject to the following constraints: |
| <ul> |
| <li> If the collection item type (for collection property) or |
| property type (for single valued property) is |
| <a href="../../../../jakarta/xml/bind/JAXBElement.html" title="class in jakarta.xml.bind"><code>JAXBElement</code></a>, then |
| <code>@XmlElementRef.name()</code> and <code>@XmlElementRef.namespace()</code> must |
| point an element factory method with an @XmlElementDecl |
| annotation in a class annotated with @XmlRegistry (usually |
| ObjectFactory class generated by the schema compiler) : |
| <ul> |
| <li> @XmlElementDecl.name() must equal @XmlElementRef.name() </li> |
| <li> @XmlElementDecl.namespace() must equal @XmlElementRef.namespace(). </li> |
| </ul> |
| </li> |
| <li> If the collection item type (for collection property) or |
| property type (for single valued property) is not |
| <a href="../../../../jakarta/xml/bind/JAXBElement.html" title="class in jakarta.xml.bind"><code>JAXBElement</code></a>, then the type referenced by the |
| property or field must be annotated with <a href="../../../../jakarta/xml/bind/annotation/XmlRootElement.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlRootElement</code></a>. </li> |
| <li> This annotation can be used with the following annotations: |
| <a href="../../../../jakarta/xml/bind/annotation/XmlElementWrapper.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlElementWrapper</code></a>, <a href="../../../../jakarta/xml/bind/annotation/adapters/XmlJavaTypeAdapter.html" title="annotation in jakarta.xml.bind.annotation.adapters"><code>XmlJavaTypeAdapter</code></a>. |
| </ul> |
| |
| <p>See "Package Specification" in jakarta.xml.bind.package javadoc for |
| additional common information.</p> |
| |
| <p><b>Example 1: Ant Task Example</b></p> |
| The following Java class hierarchy models an Ant build |
| script. An Ant task corresponds to a class in the class |
| hierarchy. The XML element name of an Ant task is indicated by the |
| @XmlRootElement annotation on its corresponding class. |
| <pre> |
| @XmlRootElement(name="target") |
| class Target { |
| // The presence of @XmlElementRef indicates that the XML |
| // element name will be derived from the @XmlRootElement |
| // annotation on the type (for e.g. "jar" for JarTask). |
| @XmlElementRef |
| List<Task> tasks; |
| } |
| |
| abstract class Task { |
| } |
| |
| @XmlRootElement(name="jar") |
| class JarTask extends Task { |
| ... |
| } |
| |
| @XmlRootElement(name="javac") |
| class JavacTask extends Task { |
| ... |
| } |
| <code> |
| |
| <!-- XML Schema fragment --> |
| <xs:element name="target" type="Target"> |
| <xs:complexType name="Target"> |
| <xs:sequence> |
| <xs:choice maxOccurs="unbounded"> |
| <xs:element ref="jar"> |
| <xs:element ref="javac"> |
| </xs:choice> |
| </xs:sequence> |
| </xs:complexType> |
| |
| </code></pre> |
| <p> |
| Thus the following code fragment: |
| <pre> |
| Target target = new Target(); |
| target.tasks.add(new JarTask()); |
| target.tasks.add(new JavacTask()); |
| marshal(target); |
| </pre> |
| will produce the following XML output: |
| <pre><code> |
| <target> |
| <jar> |
| .... |
| </jar> |
| <javac> |
| .... |
| </javac> |
| </target> |
| </code></pre> |
| <p> |
| It is not an error to have a class that extends <code>Task</code> |
| that doesn't have <a href="../../../../jakarta/xml/bind/annotation/XmlRootElement.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlRootElement</code></a>. But they can't show up in an |
| XML instance (because they don't have XML element names). |
| |
| <p><b>Example 2: XML Schema Susbstitution group support</b> |
| <p> The following example shows the annotations for XML Schema |
| substitution groups. The annotations and the ObjectFactory are |
| derived from the schema. |
| |
| <pre> |
| @XmlElement |
| class Math { |
| // The value of <a href="../../../../jakarta/xml/bind/annotation/XmlElementRef.html#type--"><code>type()</code></a>is |
| // JAXBElement.class , which indicates the XML |
| // element name ObjectFactory - in general a class marked |
| // with @XmlRegistry. (See ObjectFactory below) |
| // |
| // The <a href="../../../../jakarta/xml/bind/annotation/XmlElementRef.html#name--"><code>name()</code></a> is "operator", a pointer to a |
| // factory method annotated with a |
| // <a href="../../../../jakarta/xml/bind/annotation/XmlElementDecl.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlElementDecl</code></a> with the name "operator". Since |
| // "operator" is the head of a substitution group that |
| // contains elements "add" and "sub" elements, "operator" |
| // element can be substituted in an instance document by |
| // elements "add" or "sub". At runtime, JAXBElement |
| // instance contains the element name that has been |
| // substituted in the XML document. |
| // |
| @XmlElementRef(type=JAXBElement.class,name="operator") |
| JAXBElement<? extends Operator> term; |
| } |
| |
| @XmlRegistry |
| class ObjectFactory { |
| @XmlElementDecl(name="operator") |
| JAXBElement<Operator> createOperator(Operator o) {...} |
| @XmlElementDecl(name="add",substitutionHeadName="operator") |
| JAXBElement<Operator> createAdd(Operator o) {...} |
| @XmlElementDecl(name="sub",substitutionHeadName="operator") |
| JAXBElement<Operator> createSub(Operator o) {...} |
| } |
| |
| class Operator { |
| ... |
| } |
| </pre> |
| <p> |
| Thus, the following code fragment |
| <pre> |
| Math m = new Math(); |
| m.term = new ObjectFactory().createAdd(new Operator()); |
| marshal(m); |
| </pre> |
| will produce the following XML output: |
| <pre><code> |
| <math> |
| <add>...</add> |
| </math> |
| </code></pre></div> |
| <dl> |
| <dt><span class="simpleTagLabel">Since:</span></dt> |
| <dd>1.6, JAXB 2.0</dd> |
| <dt><span class="seeLabel">See Also:</span></dt> |
| <dd><a href="../../../../jakarta/xml/bind/annotation/XmlElementRefs.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlElementRefs</code></a></dd> |
| </dl> |
| </li> |
| </ul> |
| </div> |
| <div class="summary"> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <!-- =========== ANNOTATION TYPE OPTIONAL MEMBER SUMMARY =========== --> |
| <ul class="blockList"> |
| <li class="blockList"><a name="annotation.type.optional.element.summary"> |
| <!-- --> |
| </a> |
| <h3>Optional Element Summary</h3> |
| <table class="memberSummary" border="0" cellpadding="3" cellspacing="0" summary="Optional Element Summary table, listing optional elements, and an explanation"> |
| <caption><span>Optional Elements</span><span class="tabEnd"> </span></caption> |
| <tr> |
| <th class="colFirst" scope="col">Modifier and Type</th> |
| <th class="colLast" scope="col">Optional Element and Description</th> |
| </tr> |
| <tr class="altColor"> |
| <td class="colFirst"><code>java.lang.String</code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../../jakarta/xml/bind/annotation/XmlElementRef.html#name--">name</a></span></code> </td> |
| </tr> |
| <tr class="rowColor"> |
| <td class="colFirst"><code>java.lang.String</code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../../jakarta/xml/bind/annotation/XmlElementRef.html#namespace--">namespace</a></span></code> |
| <div class="block">This parameter and <a href="../../../../jakarta/xml/bind/annotation/XmlElementRef.html#name--"><code>name()</code></a> are used to determine the |
| XML element for the JavaBean property.</div> |
| </td> |
| </tr> |
| <tr class="altColor"> |
| <td class="colFirst"><code>boolean</code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../../jakarta/xml/bind/annotation/XmlElementRef.html#required--">required</a></span></code> |
| <div class="block">Customize the element declaration to be required.</div> |
| </td> |
| </tr> |
| <tr class="rowColor"> |
| <td class="colFirst"><code>java.lang.Class</code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../../jakarta/xml/bind/annotation/XmlElementRef.html#type--">type</a></span></code> |
| <div class="block">The Java type being referenced.</div> |
| </td> |
| </tr> |
| </table> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </div> |
| <div class="details"> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <!-- ============ ANNOTATION TYPE MEMBER DETAIL =========== --> |
| <ul class="blockList"> |
| <li class="blockList"><a name="annotation.type.element.detail"> |
| <!-- --> |
| </a> |
| <h3>Element Detail</h3> |
| <a name="type--"> |
| <!-- --> |
| </a> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <h4>type</h4> |
| <pre>public abstract java.lang.Class type</pre> |
| <div class="block">The Java type being referenced. |
| <p> |
| If the value is DEFAULT.class, the type is inferred from the |
| the type of the JavaBean property.</div> |
| <dl> |
| <dt>Default:</dt> |
| <dd>jakarta.xml.bind.annotation.XmlElementRef.DEFAULT.class</dd> |
| </dl> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| <ul class="blockList"> |
| <li class="blockList"><a name="namespace--"> |
| <!-- --> |
| </a> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <h4>namespace</h4> |
| <pre>public abstract java.lang.String namespace</pre> |
| <div class="block">This parameter and <a href="../../../../jakarta/xml/bind/annotation/XmlElementRef.html#name--"><code>name()</code></a> are used to determine the |
| XML element for the JavaBean property. |
| |
| <p> If <code>type()</code> is <code>JAXBElement.class</code> , then |
| <code>namespace()</code> and <code>name()</code> |
| point to a factory method with <a href="../../../../jakarta/xml/bind/annotation/XmlElementDecl.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlElementDecl</code></a>. The XML |
| element name is the element name from the factory method's |
| <a href="../../../../jakarta/xml/bind/annotation/XmlElementDecl.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlElementDecl</code></a> annotation or if an element from its |
| substitution group (of which it is a head element) has been |
| substituted in the XML document, then the element name is from the |
| <a href="../../../../jakarta/xml/bind/annotation/XmlElementDecl.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlElementDecl</code></a> on the substituted element. |
| |
| <p> If <a href="../../../../jakarta/xml/bind/annotation/XmlElementRef.html#type--"><code>type()</code></a> is not <code>JAXBElement.class</code>, then |
| the XML element name is the XML element name statically |
| associated with the type using the annotation <a href="../../../../jakarta/xml/bind/annotation/XmlRootElement.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlRootElement</code></a> on the type. If the type is not annotated with |
| an <a href="../../../../jakarta/xml/bind/annotation/XmlElementDecl.html" title="annotation in jakarta.xml.bind.annotation"><code>XmlElementDecl</code></a>, then it is an error. |
| |
| <p> If <code>type()</code> is not <code>JAXBElement.class</code>, then |
| this value must be "".</div> |
| <dl> |
| <dt>Default:</dt> |
| <dd>""</dd> |
| </dl> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| <ul class="blockList"> |
| <li class="blockList"><a name="name--"> |
| <!-- --> |
| </a> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <h4>name</h4> |
| <pre>public abstract java.lang.String name</pre> |
| <dl> |
| <dt><span class="seeLabel">See Also:</span></dt> |
| <dd><a href="../../../../jakarta/xml/bind/annotation/XmlElementRef.html#namespace--"><code>namespace()</code></a></dd> |
| </dl> |
| <dl> |
| <dt>Default:</dt> |
| <dd>"##default"</dd> |
| </dl> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| <ul class="blockList"> |
| <li class="blockList"><a name="required--"> |
| <!-- --> |
| </a> |
| <ul class="blockListLast"> |
| <li class="blockList"> |
| <h4>required</h4> |
| <pre>public abstract boolean required</pre> |
| <div class="block">Customize the element declaration to be required. |
| <p> |
| If required() is true, then Javabean property is mapped to |
| an XML schema element declaration with minOccurs="1". |
| maxOccurs is "1" for a single valued property and "unbounded" |
| for a multivalued property. |
| |
| <p> |
| If required() is false, then the Javabean property is mapped |
| to XML Schema element declaration with minOccurs="0". |
| maxOccurs is "1" for a single valued property and "unbounded" |
| for a multivalued property. |
| |
| <p> |
| For compatibility with Jakarta XML Binding, this property defaults to <code>true</code>, |
| despite the fact that <a href="../../../../jakarta/xml/bind/annotation/XmlElement.html#required--"><code>XmlElement.required()</code></a> defaults to false.</div> |
| <dl> |
| <dt><span class="simpleTagLabel">Since:</span></dt> |
| <dd>1.7, JAXB 2.2</dd> |
| </dl> |
| <dl> |
| <dt>Default:</dt> |
| <dd>true</dd> |
| </dl> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </div> |
| </div> |
| <!-- ========= END OF CLASS DATA ========= --> |
| <!-- ======= START OF BOTTOM NAVBAR ====== --> |
| <div class="bottomNav"><a name="navbar.bottom"> |
| <!-- --> |
| </a> |
| <div class="skipNav"><a href="#skip.navbar.bottom" title="Skip navigation links">Skip navigation links</a></div> |
| <a name="navbar.bottom.firstrow"> |
| <!-- --> |
| </a> |
| <ul class="navList" title="Navigation"> |
| <li><a href="../../../../overview-summary.html">Overview</a></li> |
| <li><a href="package-summary.html">Package</a></li> |
| <li class="navBarCell1Rev">Class</li> |
| <li><a href="package-tree.html">Tree</a></li> |
| <li><a href="../../../../deprecated-list.html">Deprecated</a></li> |
| <li><a href="../../../../index-all.html">Index</a></li> |
| <li><a href="../../../../help-doc.html">Help</a></li> |
| </ul> |
| </div> |
| <div class="subNav"> |
| <ul class="navList"> |
| <li><a href="../../../../jakarta/xml/bind/annotation/XmlElementDecl.GLOBAL.html" title="class in jakarta.xml.bind.annotation"><span class="typeNameLink">Prev Class</span></a></li> |
| <li><a href="../../../../jakarta/xml/bind/annotation/XmlElementRef.DEFAULT.html" title="class in jakarta.xml.bind.annotation"><span class="typeNameLink">Next Class</span></a></li> |
| </ul> |
| <ul class="navList"> |
| <li><a href="../../../../index.html?jakarta/xml/bind/annotation/XmlElementRef.html" target="_top">Frames</a></li> |
| <li><a href="XmlElementRef.html" target="_top">No Frames</a></li> |
| </ul> |
| <ul class="navList" id="allclasses_navbar_bottom"> |
| <li><a href="../../../../allclasses-noframe.html">All Classes</a></li> |
| </ul> |
| <div> |
| <script type="text/javascript"><!-- |
| allClassesLink = document.getElementById("allclasses_navbar_bottom"); |
| if(window==top) { |
| allClassesLink.style.display = "block"; |
| } |
| else { |
| allClassesLink.style.display = "none"; |
| } |
| //--> |
| </script> |
| </div> |
| <div> |
| <ul class="subNavList"> |
| <li>Summary: </li> |
| <li>Field | </li> |
| <li>Required | </li> |
| <li><a href="#annotation.type.optional.element.summary">Optional</a></li> |
| </ul> |
| <ul class="subNavList"> |
| <li>Detail: </li> |
| <li>Field | </li> |
| <li><a href="#annotation.type.element.detail">Element</a></li> |
| </ul> |
| </div> |
| <a name="skip.navbar.bottom"> |
| <!-- --> |
| </a></div> |
| <!-- ======== END OF BOTTOM NAVBAR ======= --> |
| </body> |
| </html> |