Spec-Zone .ru
спецификации, руководства, описания, API
Please note that the specifications and other information contained herein are not final and are subject to change. The information is being made available to you solely for purpose of evaluation.

Java™ Platform
Standard Ed. 7

DRAFT ea-b118

javax.xml.bind.annotation
Annotation Type XmlElementRef


@Retention(value=RUNTIME)
@Target(value={FIELD,METHOD})
public @interface XmlElementRef

Maps a JavaBean property to a XML element derived from property's type.

Usage

@XmlElementRef annotation can be used with a JavaBean property or from within XmlElementRefs

This annotation dynamically associates an XML element name with the JavaBean property. When a JavaBean property is annotated with XmlElement, 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.

XML Schema substitution group support

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 element property, (section 5.5.5, "Element Property" of JAXB 2.0 specification). An element property method signature is of the form:

     public void setTerm(JAXBElement<? extends Operatorgt;);
     public JAXBElement<? extends Operatorgt; getTerm();
 

An element factory method annotated with XmlElementDecl is used to create a JAXBElement instance, containing an XML element name. The presence of @XmlElementRef annotation on an element property indicates that the element name from JAXBElement instance be used instead of deriving an XML element name from the JavaBean property name.

The usage is subject to the following constraints:

See "Package Specification" in javax.xml.bind.package javadoc for additional common information.

Example 1: Ant Task Example

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.
     @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 {
         ...
     }

     <!-- 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>

 

Thus the following code fragment:

     Target target = new Target();
     target.tasks.add(new JarTask());
     target.tasks.add(new JavacTask());
     marshal(target);
 
will produce the following XML output:

     <target>
       <jar>
         ....
       </jar>
       <javac>
         ....
       </javac>
     </target>
 

It is not an error to have a class that extends Task that doesn't have XmlRootElement. But they can't show up in an XML instance (because they don't have XML element names).

Example 2: XML Schema Susbstitution group support

The following example shows the annotations for XML Schema substitution groups. The annotations and the ObjectFactory are derived from the schema.

     @XmlElement
     class Math {
         //  The value of type()is 
         //  JAXBElement.class , which indicates the XML
         //  element name ObjectFactory - in general a class marked
         //  with @XmlRegistry. (See ObjectFactory below)
         //  
         //  The name() is "operator", a pointer to a
         // factory method annotated with a
         //  XmlElementDecl 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 {
         ...
     }
 

Thus, the following code fragment

     Math m = new Math();
     m.term = new ObjectFactory().createAdd(new Operator());
     marshal(m);
 
will produce the following XML output:
     <math>
       <add>...</add>
     </math>
 

Since:
JAXB2.0
See Also:
XmlElementRefs

Optional Element Summary
Modifier and Type Optional Element and Description
 String name
           
 String namespace
          This parameter and name() are used to determine the XML element for the JavaBean property.
 boolean required
          Customize the element declaration to be required.
 Class type
          The Java type being referenced.
 

type

public abstract Class type
The Java type being referenced.

If the value is DEFAULT.class, the type is inferred from the the type of the JavaBean property.

Default:
javax.xml.bind.annotation.XmlElementRef.DEFAULT.class

namespace

public abstract String namespace
This parameter and name() are used to determine the XML element for the JavaBean property.

If type() is JAXBElement.class , then namespace() and name() point to a factory method with XmlElementDecl. The XML element name is the element name from the factory method's XmlElementDecl 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 XmlElementDecl on the substituted element.

If type() is not JAXBElement.class, then the XML element name is the XML element name statically associated with the type using the annotation XmlRootElement on the type. If the type is not annotated with an XmlElementDecl, then it is an error.

If type() is not JAXBElement.class, then this value must be "".

Default:
""

name

public abstract String name
See Also:
namespace()
Default:
"##default"

required

public abstract boolean required
Customize the element declaration to be required.

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.

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.

For compatibility with JAXB 2.1, this property defaults to true, despite the fact that XmlElement.required() defaults to false.

Since:
2.2
Default:
true

Java™ Platform
Standard Ed. 7

DRAFT ea-b118

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Copyright © 1993, 2010, Oracle Corporation. All rights reserved.
DRAFT ea-b118

Scripting on this page tracks web page traffic, but does not change the content in any way.