モジュール java.xml.bind
パッケージ javax.xml.bind.annotation

注釈型XmlAnyElement



  • @Retention(RUNTIME)
    @Target({FIELD,METHOD})
    public @interface XmlAnyElement
    JavaBeanプロパティを、XML情報セット表現またはJAXB要素、あるいはその両方にマップします。

    XMLコンテンツをJAXB注釈を付けられたクラスのインスタンスに非整列化する処理の実行中、この注釈は、「catch-all (すべてをキャッチする)」プロパティとして働きます。 通常、複数値を持つJavaBeanプロパティを注釈しますが、単一値を持つJavaBeanプロパティで使用されることもあります。 非整列化の実行中、クラスのその他のJavaBeanプロパティのstatic@XmlElementまたは@XmlElementRef注釈に一致しない各XML要素は、この「catch-all」プロパティに追加されます。

    使用法:

     @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;
     

    制限使用制約

    この注釈は、XmlElementXmlAttributeXmlValueXmlElementsXmlID、およびXmlIDREFと相互に排他的です。

    あるクラスとそれのスーパー・クラスで、XmlAnyElementで注釈されたJavaBeanプロパティは1つだけです。

    ほかの注釈との関係

    この注釈はXmlJavaTypeAdapterとともに使用できるため、ユーザーは自分のデータ構造をDOMにマップしたり、それからXMLを構成したりできます。

    この注釈はXmlMixedとともに、次のように使用できます。

     // List of java.lang.String or DOM nodes.
     @XmlAnyElement @XmlMixed
     List<Object> others;
     

    スキーマからJavaへのマップ例

    次のスキーマは、次のJavaクラスを生成します。
    
     <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;
       @XmlAnyElement
       List<Element> any;
     }
     
    次のようなインスタンスを非整列化できます。
    
     <foo xmlns:e="extra">
       <a>1</a>
       <e:other />  // this will be bound to DOM, because unmarshalling is orderless
       <b>3</b>
       <e:other />
       <c>5</c>     // this will be bound to DOM, because the annotation doesn't remember namespaces.
     </foo>
     
    次のスキーマは、次のJavaクラスを生成します。
    
     <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.
     }
     
    次のようなインスタンスを非整列化できます。
    
     <bar xmlns:e="extra">
       <a>1</a>
       <e:other />  // this will be bound to DOM, because unmarshalling is orderless
       <b>3</b>
       <e:other />
       <c>5</c>     // this now goes to Bar.c
       <e:other />  // this will go to Foo.any
     </bar>
     

    XmlAnyElementXmlElementRefの使用

    XmlAnyElement注釈をXmlElementRefとともに使用して、コンテンツ・ツリーに参加可能な追加要素を指定できます。

    次のスキーマは、次のJavaクラスを生成します。

    
     <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 {
       @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 ) { ... }
     
    次のようなインスタンスを非整列化できます。
    <foo xmlns:e="extra">
      <a>1</a>     // this will unmarshal to a JAXBElement instance whose value is 1.
      <e:other />  // this will unmarshal to a DOM Element.
      <b>3</b>     // this will unmarshal to a JAXBElement instance whose value is 1.
    </foo>
     

    W3C XML Schema「lax」ワイルドカード・エミュレーション

    この注釈のlax要素は、「lax」ワイルドカード・セマンティクスのエミュレーションを可能にします。 たとえば、Javaソース・コードが次のように注釈された場合を考えます。
     @XmlRootElement
     class Foo {
       @XmlAnyElement(lax=true)
       public Object[] 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;
     
    導入されたバージョン:
    1.6, JAXB 2.0
    • オプション要素のサマリー

      オプションの要素 
      修飾子と型 オプション要素 説明
      boolean lax
      現在のJAXBContextで既知の要素が検出された場合、unmarshallerの動作を制御します。
      Class<? extends DomHandler> value
      XMLと、DOMに似たデータ構造の間の変換を実際に行う、DomHandlerを指定します。
    • 要素の詳細

      • lax

        boolean lax
        現在のJAXBContextで既知の要素が検出された場合、unmarshallerの動作を制御します。

        falseの場合

        falseの場合、プロパティに一致するすべての要素がDOMに非整列化され、プロパティにはDOM要素のみが含まれます。

        trueの場合

        trueの場合、要素がXmlAnyElementでマークされているプロパティに一致し、JAXBContextに知られている場合(たとえば、同じタグ名を持つXmlRootElementを伴うクラスが存在する場合や、同じタグ名を持つXmlElementDeclが存在する場合)、unmarshallerはこの要素をDOMに非整列化するかわりに、それをJAXBオブジェクトに非整列化しようとします。 また、要素は不明であるがそれが既知のxsi:typeを持つ場合、unmarshallerはその要素をJAXBElementに非整列化しようとします。これは不明な要素名を持ち、JAXB要素の値は既知のxsi:typeのJAXBマッピングのインスタンスに設定されます。

        結果として、非整列化後、プロパティは異種的なものとなり、DOMノードとJAXBオブジェクトを同時に持つ可能性があります。

        これを使用すると、W3C XML Schemaの「lax」ワイルドカード・セマンティクスをエミュレートできます。

        デフォルト:
        false
      • value

        Class<? extends DomHandler> value
        XMLと、DOMに似たデータ構造の間の変換を実際に行う、DomHandlerを指定します。
        デフォルト:
        javax.xml.bind.annotation.W3CDomHandler.class