XmlElements (Java EE 5)

Java EE API


javax.xml.bind.annotation Annotation Type XmlElements


@Retention(value=RUNTIME)
@Target(value={FIELD,METHOD})
public @interface XmlElements

Implements: Annotation
@Retention(value=RUNTIME)
@Target(value={FIELD, METHOD})

多个 @XmlElement 注释的容器。 在一个程序元素上不允许使用多个相同类型的注释。因此此注释用于充当多个 @XmlElements 的容器注释,如下所示:

 @XmlElements({ @XmlElement(...),@XmlElement(...) })
 

@XmlElements 注释可以与以下程序元素一起使用:

  • JavaBean 属性
  • 非 static、非 transient 字段
此注释用于注释 JavaBean 集合属性(如列表)。

用法

用法受到以下约束的限制:

  • 此注释可与以下注释一起使用:@XmlIDREF 和 @XmlElementWrapper
  • 如果将 @XmlIDREF 也指定到 JavaBean 属性,那么每个 @XmlElement.type() 都必须包含使用 @XmlID 注释的 JavaBean 属性。

有关其他公共信息,请参阅 javax.xml.bind.package javadoc 中的“包规范”。


示例 1:映射到元素列表

    
    // Mapped code fragment
    public class Foo {
        @XmlElements(
            @XmlElement(name="A", type=Integer.class),
            @XmlElement(name="B", type=Float.class)
         }
         public List items;
    }

    <!-- XML Representation for a List of {1,2.5} 
            XML output is not wrapped using another element -->
    ...
     1 
     2.5 
    ...

    <!-- XML Schema fragment -->
    <xs:complexType name="Foo">
      <xs:sequence>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="A" type="xs:int"/>
          <xs:element name="B" type="xs:float"/>
        <xs:choice>
      </xs:sequence>
    </xs:complexType>

 

示例 2:映射到使用另一个元素包装的元素列表

 
    // Mapped code fragment
    public class Foo {
        @XmlElementWrapper(name="bar")
        @XmlElements(
            @XmlElement(name="A", type=Integer.class),
            @XmlElement(name="B", type=Float.class)
        }
        public List items;
    }

    <!-- XML Schema fragment -->
    <xs:complexType name="Foo">
      <xs:sequence>
        <xs:element name="bar">
          <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
              <xs:element name="A" type="xs:int"/>
              <xs:element name="B" type="xs:float"/>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
 

示例 3:根据类型使用适配器更改元素名称。

    class Foo {
       @XmlJavaTypeAdapter(QtoPAdapter.class)
       @XmlElements({
           @XmlElement(name="A",type=PX.class),
           @XmlElement(name="B",type=PY.class)
       })
       Q bar;
    }
 
    @XmlType abstract class P {...}
    @XmlType(name="PX") class PX extends P {...}
    @XmlType(name="PY") class PY extends P {...}

    <!-- XML Schema fragment -->
    <xs:complexType name="Foo">
      <xs:sequence>
        <xs:element name="bar">
          <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
              <xs:element name="A" type="PX"/>
              <xs:element name="B" type="PY"/>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
 
英文文档:

A container for multiple @XmlElement annotations. Multiple annotations of the same type are not allowed on a program element. This annotation therefore serves as a container annotation for multiple @XmlElements as follows:

 @XmlElements({ @XmlElement(...),@XmlElement(...) })
 

The @XmlElements annnotation can be used with the following program elements:

  • a JavaBean property
  • non static, non transient field
This annotation is intended for annotation a JavaBean collection property (e.g. List).

Usage

The usage is subject to the following constraints:

  • This annotation can be used with the following annotations: @XmlIDREF, @XmlElementWrapper.
  • If @XmlIDREF is also specified on the JavaBean property, then each @XmlElement.type() must contain a JavaBean property annotated with @XmlID.

See "Package Specification" in javax.xml.bind.package javadoc for additional common information.


Example 1: Map to a list of elements

    
    // Mapped code fragment
    public class Foo {
        @XmlElements(
            @XmlElement(name="A", type=Integer.class),
            @XmlElement(name="B", type=Float.class)
         }
         public List items;
    }

    <!-- XML Representation for a List of {1,2.5} 
            XML output is not wrapped using another element -->
    ...
     1 
     2.5 
    ...

    <!-- XML Schema fragment -->
    <xs:complexType name="Foo">
      <xs:sequence>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="A" type="xs:int"/>
          <xs:element name="B" type="xs:float"/>
        <xs:choice>
      </xs:sequence>
    </xs:complexType>

 

Example 2: Map to a list of elements wrapped with another element

 
    // Mapped code fragment
    public class Foo {
        @XmlElementWrapper(name="bar")
        @XmlElements(
            @XmlElement(name="A", type=Integer.class),
            @XmlElement(name="B", type=Float.class)
        }
        public List items;
    }

    <!-- XML Schema fragment -->
    <xs:complexType name="Foo">
      <xs:sequence>
        <xs:element name="bar">
          <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
              <xs:element name="A" type="xs:int"/>
              <xs:element name="B" type="xs:float"/>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
 

Example 3: Change element name based on type using an adapter.

    class Foo {
       @XmlJavaTypeAdapter(QtoPAdapter.class)
       @XmlElements({
           @XmlElement(name="A",type=PX.class),
           @XmlElement(name="B",type=PY.class)
       })
       Q bar;
    }
 
    @XmlType abstract class P {...}
    @XmlType(name="PX") class PX extends P {...}
    @XmlType(name="PY") class PY extends P {...}

    <!-- XML Schema fragment -->
    <xs:complexType name="Foo">
      <xs:sequence>
        <xs:element name="bar">
          <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
              <xs:element name="A" type="PX"/>
              <xs:element name="B" type="PY"/>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
 

Since:
JAXB2.0
Author:
  • Kohsuke Kawaguchi, Sun Microsystems, Inc.
  • Sekhar Vajjhala, Sun Microsystems, Inc.
See Also:
XmlElement, XmlElementRef, XmlElementRefs, XmlJavaTypeAdapter

Required Element Summary
 XmlElement[]
 

Element Detail

abstract public XmlElement[] value()
@XmlElement 注释的集合。
英文文档:

value

public abstract XmlElement[] value
Collection of @XmlElement annotations



Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!