XmlAnyElement (Java Platform SE 8 ) (original) (raw)
Maps a JavaBean property to XML infoset representation and/or JAXB element.
This annotation serves as a "catch-all" property while unmarshalling xml content into a instance of a JAXB annotated class. It typically annotates a multi-valued JavaBean property, but it can occur on single value JavaBean property. During unmarshalling, each xml element that does not match a static @XmlElement or @XmlElementRef annotation for the other JavaBean properties on the class, is added to this "catch-all" property.
Usages:
@XmlAnyElement public Element[] others;
// Collection of Element or JAXB elements. @XmlAnyElement(lax="true") public Object[] others;
@XmlAnyElement private List<Element> nodes;
@XmlAnyElement private Element node;
Restriction usage constraints
This annotation is mutually exclusive withXmlElement, XmlAttribute, XmlValue,XmlElements, XmlID, and XmlIDREF.
There can be only one XmlAnyElement annotated JavaBean property in a class and its super classes.
Relationship to other annotations
This annotation can be used with XmlJavaTypeAdapter, so that users can map their own data structure to DOM, which in turn can be composed into XML.
This annotation can be used with XmlMixed like this:
// List of java.lang.String or DOM nodes. @XmlAnyElement @XmlMixed List others;
Schema To Java example
The following schema would produce the following Java class:
<xs:complexType name="foo"> xs:sequence <xs:element name="a" type="xs:int" /> <xs:element name="b" type="xs:int" /> <xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded" />
class Foo { int a; int b; @XmlAnyElement List any; }
It can unmarshal instances like
1 // this will be bound to DOM, because unmarshalling is orderless 3 5 // this will be bound to DOM, because the annotation doesn't remember namespaces.The following schema would produce the following Java class:
<xs:complexType name="bar"> xs:complexContent <xs:extension base="foo"> xs:sequence <xs:element name="c" type="xs:int" /> <xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded" />
class Bar extends Foo { int c; // Foo.getAny() also represents wildcard content for type definition bar. }
It can unmarshal instances like
1 // this will be bound to DOM, because unmarshalling is orderless 3 5 // this now goes to Bar.c // this will go to Foo.anyUsing XmlAnyElement with XmlElementRef
The XmlAnyElement annotation can be used with XmlElementRefs to designate additional elements that can participate in the content tree.
The following schema would produce the following Java class:
<xs:complexType name="foo"> <xs:choice maxOccurs="unbounded" minOccurs="0"> <xs:element name="a" type="xs:int" /> <xs:element name="b" type="xs:int" /> <xs:any namespace="##other" processContents="lax" />
class Foo { @XmlAnyElement(lax="true") @XmlElementRefs({ @XmlElementRef(name="a", type="JAXBElement.class") @XmlElementRef(name="b", type="JAXBElement.class") }) List<Object> others; }
@XmlRegistry class ObjectFactory { ... @XmlElementDecl(name = "a", namespace = "", scope = Foo.class) JAXBElement createFooA( Integer i ) { ... }
@XmlElementDecl(name = "b", namespace = "", scope = Foo.class) JAXBElement createFooB( Integer i ) { ... }
It can unmarshal instances like
1 // this will unmarshal to a [JAXBElement](../../../../javax/xml/bind/JAXBElement.html "class in javax.xml.bind") instance whose value is 1. // this will unmarshal to a DOM [Element](../../../../org/w3c/dom/Element.html "interface in org.w3c.dom"). 3 // this will unmarshal to a [JAXBElement](../../../../javax/xml/bind/JAXBElement.html "class in javax.xml.bind") instance whose value is 1.W3C XML Schema "lax" wildcard emulation
The lax element of the annotation enables the emulation of the "lax" wildcard semantics. For example, when the Java source code is annotated like this:
@XmlRootElement class Foo { @XmlAnyElement(lax=true) public Object[] others; }
then the following document will unmarshal like this:
Foo foo = unmarshal(); // 1 for 'unknown', another for 'foo' assert foo.others.length==2; // 'unknown' unmarshals to a DOM element assert foo.others[0] instanceof Element; // because of lax=true, the 'foo' element eagerly // unmarshals to a Foo object. assert foo.others[1] instanceof Foo;