-
@Retention(RUNTIME) @Target({字段,METHOD}) public @interface XmlAnyElement
将JavaBean属性映射到XML信息集表示和/或JAXB元素。当将xml内容解组到JAXB注释类的实例中时,此注释用作“全部”属性。 它通常会注释多值JavaBean属性,但它可以发生在单值JavaBean属性上。 在解组时,与该类上其他JavaBean属性的静态@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。一个类中只能有一个注释的JavaBean属性,它的超类可以只有一个
XmlAnyElement。与其他注释的关系
该注释可以与
XmlJavaTypeAdapter一起使用,以便用户可以将自己的数据结构映射到DOM,而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; @它可以解散实例XmlAnyElementList<Element> any; }以下模式将生成以下Java类:<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><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>使用
XmlAnyElement与XmlElementRefXmlAnyElement注释可以与XmlElementRefs一起使用,以指定可以参与内容树的其他元素。以下模式将生成以下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 aJAXBElementinstance whose value is 1.<e:other />// this will unmarshal to a DOMElement.<b>3</b>// this will unmarshal to aJAXBElementinstance whose value is 1.</foo>W3C XML Schema“lax”通配符仿真
注释的松散元素能够模拟“松散”通配符语义。 例如,Java源代码如下注释:@那么以下文档将如下解密: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;- 从以下版本开始:
- 1.6,JAXB 2.0
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element 描述 booleanlax当它看到当前的JAXBContext已知的元素时,控制unmarshaller的行为。Class<? extends DomHandler>value指定DomHandler,它负责将XML实际转换为类似DOM的数据结构。
-
-
-
Element Detail
-
lax
boolean lax
当它看到当前的JAXBContext已知的元素时,控制unmarshaller的行为。当假的
如果为false,则与DOM属性匹配的所有元素都将被取消组合为DOM,该属性将只包含DOM元素。
当真的
如果为true,当元素匹配标记为
XmlAnyElement的属性为JAXBContext(例如,具有XmlRootElement的类具有相同的标签名称,或者具有相同标记名称的XmlElementDecl)时,解组器将热切地解密该元素到JAXB对象,而不是将其解组到DOM。 此外,如果元素未知但是具有已知的xsi:type,则解组器会将元素强制解组为JAXBElement,其中未知元素名称和JAXBElement值设置为已知xsi:type的JAXB映射的实例。结果,在解组之后,财产可能变得异质性; 它可以同时拥有DOM节点和一些JAXB对象。
这可以用来模拟W3C XML Schema的“宽松”通配符语义。
- Default:
- 假
-
-
-
value
Class<? extends DomHandler> value
指定DomHandler,负责将XML实际转换为类似DOM的数据结构。- Default:
- javax.xml.bind.annotation.W3CDomHandler.class
-
-