| 
 | JavaTM Platform Standard Ed. 6 | |||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 必需 | 可选 | 详细信息: 元素 | |||||||||
@Retention(value=RUNTIME)
@Target(value={FIELD,METHOD})
public @interface XmlAnyElement
   将 JavaBean 属性映射到 XML 信息集表示形式和/或 JAXB 元素。
在将 xml 内容解组到 JAXB 注释类的实例中时,此注释充当 "catch-all" 属性。它通常注释多值的 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 互斥。 
 在类及其超类中只能有一个 XmlAnyElement 注释的 JavaBean 属性。 
 此注释可与 XmlJavaTypeAdapter 一起使用,以便用户能够将他们自己的数据结构映射到 DOM,然后可将这些 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;
 }
  它可以按如下方式解组实例: 
  以下模式将生成以下 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> 
XmlElementRef 与 XmlAnyElement 一起使用 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 <A HREF="../../../../javax/xml/bind/JAXBElement.html" title="javax.xml.bind 中的类"><CODE>JAXBElement</CODE></A> instance whose value is 1. <e:other /> // this will unmarshal to a DOM <A HREF="../../../../org/w3c/dom/Element.html" title="org.w3c.dom 中的接口"><CODE>Element</CODE></A>. <b>3</b> // this will unmarshal to a <A HREF="../../../../javax/xml/bind/JAXBElement.html" title="javax.xml.bind 中的类"><CODE>JAXBElement</CODE></A> instance 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中的已知元素时控制解组行为。 | 
|  Class<? extends DomHandler> | value指定 DomHandler,它负责实际执行 XML 与类 DOM 数据结构之间的转换。 | 
public abstract boolean lax
JAXBContext 中的已知元素时控制解组行为。 
    如果为 false,则所有与该属性匹配的元素都将被解组为 DOM,并且该属性将只包含 DOM 元素。
 如果为 true,则当某一元素与使用 JAXBContext 已知的 XmlAnyElement 标记的属性匹配时(例如,存在具有相同标记名称的带有 XmlRootElement 的类,或者存在具有相同标记名称的 XmlElementDecl),unmarshaller 会立即将此元素解组到 JAXB 对象,而不是解组到 DOM。此外,如果元素是未知的,但它有一个已知的 xsi:type,则通过使用未知的元素名称和设置为已知 xsi:type 的 JAXB 映射实例的 JAXBElement 值,unmarshaller 可立即将此元素解组到 JAXBElement。 
因此,在解组之后,属性可能变得完全不同,它可能在包含 DOM 节点的同时还包含一些 JAXB 对象。
可以使用这种特性来仿真 W3C XML 模式的 "lax" 通配符语义。
public abstract Class<? extends DomHandler> value
DomHandler,它负责实际执行 XML 与类 DOM 数据结构之间的转换。 
    
| 
 | JavaTM Platform Standard Ed. 6 | |||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 必需 | 可选 | 详细信息: 元素 | |||||||||
版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。