-
@Retention(value=RUNTIME) @Target(value={FIELD,METHOD}) public @interface XmlAnyElement
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 ofElement
or JAXB elements. @XmlAnyElement(lax="true") publicObject
[] others; @XmlAnyElement private List<Element
> nodes; @XmlAnyElement privateElement
node;Restriction usage constraints
This annotation is mutually exclusive with
XmlElement
,XmlAttribute
,XmlValue
,XmlElements
,XmlID
, andXmlIDREF
.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<Object> 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" /> </xs:sequence> </xs:complexType>
class Foo { int a; int b; @
It can unmarshal instances likeXmlAnyElement
List<Element> any; }<foo xmlns:e="extra"> <a>1 <e:other /> // this will be bound to DOM, because unmarshalling is orderless <b>3 <e:other /> <c>5 // this will be bound to DOM, because the annotation doesn't remember namespaces. </foo>
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" /> </xs:sequence> </xs:extension> </xs:complexType>
class Bar extends Foo { int c; // Foo.getAny() also represents wildcard content for type definition bar. }
It can unmarshal instances like<bar xmlns:e="extra"> <a>1 <e:other /> // this will be bound to DOM, because unmarshalling is orderless <b>3 <e:other /> <c>5 // this now goes to Bar.c <e:other /> // this will go to Foo.any </bar>
Using XmlAnyElement with XmlElementRef
The
XmlAnyElement
annotation can be used withXmlElementRef
s 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" /> </xs:choice> </xs:complexType>
class Foo { @
It can unmarshal instances likeXmlAnyElement
(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
<Integer> createFooA( Integer i ) { ... } @XmlElementDecl(name = "b", namespace = "", scope = Foo.class)JAXBElement
<Integer> createFooB( Integer i ) { ... }<foo xmlns:e="extra"> <a>1 // this will unmarshal to a
JAXBElement
instance whose value is 1. <e:other /> // this will unmarshal to a DOMElement
. <b>3 // this will unmarshal to aJAXBElement
instance whose value is 1. </foo>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:@
then the following document will unmarshal like this:XmlRootElement
class Foo { @XmlAnyElement(lax=true) publicObject
[] others; }<foo> <unknown /> <foo /> </foo> 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;
- Since:
- JAXB2.0
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element and Description boolean
lax
Controls the unmarshaller behavior when it sees elements known to the currentJAXBContext
.Class<? extends DomHandler>
value
Specifies theDomHandler
which is responsible for actually converting XML from/to a DOM-like data structure.
-
-
-
Element Detail
-
lax
public abstract boolean lax
Controls the unmarshaller behavior when it sees elements known to the currentJAXBContext
.When false
If false, all the elements that match the property will be unmarshalled to DOM, and the property will only contain DOM elements.
When true
If true, when an element matches a property marked with
XmlAnyElement
is known toJAXBContext
(for example, there's a class withXmlRootElement
that has the same tag name, or there'sXmlElementDecl
that has the same tag name), the unmarshaller will eagerly unmarshal this element to the JAXB object, instead of unmarshalling it to DOM. Additionally, if the element is unknown but it has a known xsi:type, the unmarshaller eagerly unmarshals the element to aJAXBElement
, with the unknown element name and the JAXBElement value is set to an instance of the JAXB mapping of the known xsi:type.As a result, after the unmarshalling, the property can become heterogeneous; it can have both DOM nodes and some JAXB objects at the same time.
This can be used to emulate the "lax" wildcard semantics of the W3C XML Schema.
- Default:
- false
-
value
public abstract Class<? extends DomHandler> value
Specifies theDomHandler
which is responsible for actually converting XML from/to a DOM-like data structure.- Default:
- javax.xml.bind.annotation.W3CDomHandler.class
-
-
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 11/06/2005 gemaakt, de laatste keer de 04/03/2020 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-rf-javax/xml/bind/annotation/xmlanyelement.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.