@Retention(value=RUNTIME) @Target(value={FIELD,METHOD}) public @interface XmlAnyElement
XMLコンテンツをJAXB注釈を付けられたクラスのインスタンスに非整列化する処理の実行中、この注釈は、「catch-all (すべてをキャッチする)」プロパティとして働きます。 通常、複数値を持つJavaBeanプロパティを注釈しますが、単一値を持つJavaBeanプロパティで使用されることもあります。 非整列化の実行中、クラスのその他のJavaBeanプロパティのstatic@XmlElementまたは@XmlElementRef注釈に一致しない各XML要素は、この「catch-all」プロパティに追加されます。
@XmlAnyElement publicElement[] others; // Collection ofElementor JAXB elements. @XmlAnyElement(lax="true") publicObject[] others; @XmlAnyElement private List<Element> nodes; @XmlAnyElement privateElementnode;
この注釈は、XmlElement、XmlAttribute、XmlValue、XmlElements、XmlID、およびXmlIDREFと相互に排他的です。
あるクラスとそれのスーパー・クラスで、XmlAnyElementで注釈されたJavaBeanプロパティは1つだけです。
この注釈はXmlJavaTypeAdapterとともに使用できるため、ユーザーは自分のデータ構造をDOMにマップしたり、それからXMLを構成したりできます。
この注釈はXmlMixedとともに、次のように使用できます。
// List of java.lang.String or DOM nodes. @XmlAnyElement @XmlMixed List<Object> others;
<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 <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>次のスキーマは、次の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 <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>
XmlAnyElementとXmlElementRefの使用
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 // this will unmarshal to aJAXBElementinstance whose value is 1. <e:other /> // this will unmarshal to a DOMElement. <b>3 // this will unmarshal to aJAXBElementinstance whose value is 1. </foo>
@次の文書は次のように非整列化されます。XmlRootElementclass 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;
| 修飾子と型 | オプション要素 | 説明 |
|---|---|---|
boolean |
lax |
現在の
JAXBContextで既知の要素が検出された場合、unmarshallerの動作を制御します。 |
Class<? extends DomHandler> |
value |
XMLと、DOMに似たデータ構造の間の変換を実際に行う、
DomHandlerを指定します。 |
public abstract boolean lax
JAXBContextで既知の要素が検出された場合、unmarshallerの動作を制御します。
falseの場合、プロパティに一致するすべての要素がDOMに非整列化され、プロパティにはDOM要素のみが含まれます。
trueの場合、要素がXmlAnyElementでマークされているプロパティに一致し、JAXBContextに知られている場合(たとえば、同じタグ名を持つXmlRootElementを伴うクラスが存在する場合や、同じタグ名を持つXmlElementDeclが存在する場合)、unmarshallerはこの要素をDOMに非整列化するかわりに、それをJAXBオブジェクトに非整列化しようとします。 また、要素は不明であるがそれが既知のxsi:typeを持つ場合、unmarshallerはその要素をJAXBElementに非整列化しようとします。これは不明な要素名を持ち、JAXB要素の値は既知のxsi:typeのJAXBマッピングのインスタンスに設定されます。
結果として、非整列化後、プロパティは異種的なものとなり、DOMノードとJAXBオブジェクトを同時に持つ可能性があります。
これを使用すると、W3C XML Schemaの「lax」ワイルドカード・セマンティクスをエミュレートできます。
public abstract Class<? extends DomHandler> value
DomHandlerを指定します。 バグまたは機能を送信
詳細なAPIリファレンスおよび開発者ドキュメントについては、Java SEのドキュメントを参照してください。 そのドキュメントには、概念的な概要、用語の定義、回避方法、有効なコード例などの、開発者を対象にしたより詳細な説明が含まれています。
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Documentation Redistribution Policyも参照してください。