XmlAnyElement (Java EE 5 SDK)

Java

Java EE 5 SDK

深圳电信培训中心.徐海蛟老师.


javax.xml.bind.annotation Annotation Type XmlAnyElement


@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 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 with XmlElement, 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<Object> others;
 

Schema To Java example

The following schema would produce the following Java class:

 &lt;xs:complextype name="foo"&gt;
   &lt;xs:sequence&gt;
     &lt;xs:element name="a" type="xs:int"&gt;
     &lt;/xs:element&gt;&lt;xs:element name="b" type="xs:int"&gt;
     &lt;xs:any namespace="##other" processcontents="lax" minoccurs="0" maxoccurs="unbounded"&gt;
   &lt;/xs:any&gt;&lt;/xs:element&gt;&lt;/xs:sequence&gt;
 &lt;/xs:complextype&gt;
 
 class Foo {
   int a;
   int b;
   @XmlAnyElement
   List<Element> any;
 }
 
It can unmarshal instances like

 &lt;foo xmlns:e="extra"&gt;
   &lt;a&gt;1&lt;/a&gt;
   &lt;e:other&gt;  // this will be bound to DOM, because unmarshalling is orderless
   &lt;b&gt;3&lt;/b&gt;
   &lt;/e:other&gt;&lt;e:other&gt;
   &lt;c&gt;5&lt;/c&gt;     // this will be bound to DOM, because the annotation doesn't remember namespaces.
 &lt;/e:other&gt;&lt;/foo&gt;
 
The following schema would produce the following Java class:

 &lt;xs:complextype name="bar"&gt;
   &lt;xs:complexcontent&gt;
   &lt;xs:extension base="foo"&gt;
     &lt;xs:sequence&gt;
       &lt;xs:element name="c" type="xs:int"&gt;
       &lt;xs:any namespace="##other" processcontents="lax" minoccurs="0" maxoccurs="unbounded"&gt;
     &lt;/xs:any&gt;&lt;/xs:element&gt;&lt;/xs:sequence&gt;
   &lt;/xs:extension&gt;
 &lt;/xs:complexcontent&gt;&lt;/xs:complextype&gt;
 

 class Bar extends Foo {
   int c;
   // Foo.getAny() also represents wildcard content for type definition bar.
 }
 
It can unmarshal instances like

 &lt;bar xmlns:e="extra"&gt;
   &lt;a&gt;1&lt;/a&gt;
   &lt;e:other&gt;  // this will be bound to DOM, because unmarshalling is orderless
   &lt;b&gt;3&lt;/b&gt;
   &lt;/e:other&gt;&lt;e:other&gt;
   &lt;c&gt;5&lt;/c&gt;     // this now goes to Bar.c
   &lt;/e:other&gt;&lt;e:other&gt;  // this will go to Foo.any
 &lt;/e:other&gt;&lt;/bar&gt;
 

Using 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:


 &lt;xs:complextype name="foo"&gt;
   &lt;xs:choice maxoccurs="unbounded" minoccurs="0"&gt;
     &lt;xs:element name="a" type="xs:int"&gt;
     &lt;/xs:element&gt;&lt;xs:element name="b" type="xs:int"&gt;
     &lt;xs:any namespace="##other" processcontents="lax"&gt;
   &lt;/xs:any&gt;&lt;/xs:element&gt;&lt;/xs:choice&gt;
 &lt;/xs:complextype&gt;
 
 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<Integer> createFooA( Integer i ) { ... }

   @XmlElementDecl(name = "b", namespace = "", scope = Foo.class)
   JAXBElement<Integer> createFooB( Integer i ) { ... }
 
It can unmarshal instances like

 &lt;foo xmlns:e="extra"&gt;
   &lt;a&gt;1&lt;/a&gt;     // this will unmarshal to a &lt;a href="../../../../javax/xml/bind/JAXBElement.html" title="class in javax.xml.bind"&gt;&lt;code&gt;JAXBElement&lt;/code&gt;&lt;/a&gt; instance whose value is 1.
   &lt;e:other&gt;  // this will unmarshal to a DOM &lt;a href="http://java.sun.com/j2se/1.5/docs/api/org/w3c/dom/Element.html" title="class or interface in org.w3c.dom"&gt;&lt;code&gt;Element&lt;/code&gt;&lt;/a&gt;.
   &lt;b&gt;3&lt;/b&gt;     // this will unmarshal to a &lt;a href="../../../../javax/xml/bind/JAXBElement.html" title="class in javax.xml.bind"&gt;&lt;code&gt;JAXBElement&lt;/code&gt;&lt;/a&gt; instance whose value is 1.
 &lt;/e:other&gt;&lt;/foo&gt;
 

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:

 &lt;foo&gt;
   &lt;unknown&gt;
   &lt;/unknown&gt;&lt;/foo&gt;&lt;foo&gt;
 &lt;/foo&gt;

 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;
 

从以下版本开始:
JAXB2.0
作者:
Kohsuke Kawaguchi

Optional Element Summary
 boolean lax
          Controls the unmarshaller behavior when it sees elements known to the current JAXBContext.
 Class<? extends DomHandler> value
          Specifies the DomHandler which is responsible for actually converting XML from/to a DOM-like data structure.
 

lax

public abstract boolean lax
Controls the unmarshaller behavior when it sees elements known to the current JAXBContext.

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 to JAXBContext (for example, there's a class with XmlRootElement that has the same tag name, or there's XmlElementDecl 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 a JAXBElement, 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.

默认值:
false

value

public abstract Class<? extends DomHandler> value
Specifies the DomHandler which is responsible for actually converting XML from/to a DOM-like data structure.

默认值:
javax.xml.bind.annotation.W3CDomHandler.class

Java EE 5 SDK

深圳电信培训中心.徐海蛟老师.


提交错误或意见

版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。深圳电信培训中心.徐海蛟老师教学参考.