Annotation Type XmlElementRef
-
@Retention(RUNTIME) @Target({FIELD,METHOD}) public @interface XmlElementRefMaps a JavaBean property to an XML element derived from property's type.
Usage
@XmlElementRefannotation can be used with a JavaBean property or from withinXmlElementRefsThis 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 an 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 Jakarta XML Binding specification). An element property method signature is of the form:An element factory method annotated with
XmlElementDeclis used to create aJAXBElementinstance, containing an XML element name. The presence of@XmlElementRefannotation on an element property indicates that the element name fromJAXBElementinstance 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
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) :- @XmlElementDecl.name() must equal @XmlElementRef.name()
- @XmlElementDecl.namespace() must equal @XmlElementRef.namespace().
- If the collection item type (for collection property) or
property type (for single valued property) is not
JAXBElement, then the type referenced by the property or field must be annotated withXmlRootElement. - This annotation can be used with the following annotations:
XmlElementWrapper,XmlJavaTypeAdapter.
See "Package Specification" in jakarta.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. {@snippet :- Since:
- 1.6, JAXB 2.0
- Author:
- Kohsuke Kawaguchi, Sun Microsystems,Inc.
- Sekhar Vajjhala, Sun Microsystems, Inc.
- See Also:
XmlElementRefs
- If the collection item type (for collection property) or
property type (for single valued property) is
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description java.lang.Stringnamejava.lang.StringnamespaceThis parameter andname()are used to determine the XML element for the JavaBean property.booleanrequiredCustomize the element declaration to be required.java.lang.Class<?>typeThe Java type being referenced.
-
-
-
-
namespace
java.lang.String namespace
This parameter andname()are used to determine the XML element for the JavaBean property.If
type()isJAXBElement.class, thennamespace()andname()point to a factory method withXmlElementDecl. The XML element name is the element name from the factory method'sXmlElementDeclannotation 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 theXmlElementDeclon the substituted element.If
type()is notJAXBElement.class, then the XML element name is the XML element name statically associated with the type using the annotationXmlRootElementon the type. If the type is not annotated with anXmlElementDecl, then it is an error.If
type()is notJAXBElement.class, then this value must be "".- Default:
- ""
-
-
-
name
java.lang.String name
- See Also:
namespace()
- Default:
- "##default"
-
-
-
required
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 Jakarta XML Binding, this property defaults to
true, despite the fact thatXmlElement.required()defaults to false.- Since:
- 1.7, JAXB 2.2
- Default:
- true
-
-