XmlElementRef.javaAPI DocJava SE 6 API9240Tue Jun 10 00:27:04 BST 2008javax.xml.bind.annotation


public class XmlElementRef

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


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

This annotation dynamically associates an XML element name with the JavaBean property. When a JavaBean property is annotated with {@link 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 Operator>);
public JAXBElement<? extends Operator> getTerm();

An element factory method annotated with {@link 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:

  • If the collection item type (for collection property) or property type (for single valued property) is {@link javax.xml.bind.JAXBElement}, then @XmlElementRef}.name() and @XmlElementRef.namespace() must point an element factory method with an @XmlElementDecl annotation in a class annotated with @XmlRegistry (usually ObjectFactory class generated by the schema compiler) :
    • must equal
    • @XmlElementDecl.namespace() must equal @XmlElementRef.namespace().
  • If the collection item type (for collection property) or property type (for single valued property) is not {@link javax.xml.bind.JAXBElement}, then the type referenced by the property or field must be annotated with {@link XmlRootElement}.
  • This annotation can be used with the following annotations: {@link XmlElementWrapper}, {@link XmlJavaTypeAdapter}.

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.
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).
List<Task> tasks;

abstract class Task {

class JarTask extends Task {

class JavacTask extends Task {

<!-- XML Schema fragment -->
<xs:element name="target" type="Target">
<xs:complexType name="Target">
<xs:choice maxOccurs="unbounded">
<xs:element ref="jar">
<xs:element ref="javac">

Thus the following code fragment:

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


It is not an error to have a class that extends Task that doesn't have {@link 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.

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

class ObjectFactory {
JAXBElement<Operator> createOperator(Operator o) {...}
JAXBElement<Operator> createAdd(Operator o) {...}
JAXBElement<Operator> createSub(Operator o) {...}

class Operator {

Thus, the following code fragment

Math m = new Math();
m.term = new ObjectFactory().createAdd(new Operator());
will produce the following XML output:
  • Kohsuke Kawaguchi, Sun Microsystems,Inc.
  • Sekhar Vajjhala, Sun Microsystems, Inc.

Fields Summary
Constructors Summary
Methods Summary


This parameter and {@link #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 {@link XmlElementDecl}. The XML element name is the element name from the factory method's {@link 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 {@link XmlElementDecl} on the substituted element.

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

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

The Java type being referenced.

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