XmlElementRef (Java EE 5)

Java EE API


javax.xml.bind.annotation Annotation Type XmlElementRef


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

Implements: Annotation
Inner classes: XmlElementRef.DEFAULT
@Retention(value=RUNTIME)
@Target(value={FIELD, METHOD})

将 JavaBean 属性映射到派生于属性类型的 XML 元素。

使用

@XmlElementRef 注释可以与 JavaBean 属性一起使用或在 XmlElementRefs 中使用。

此注释将 XML 元素名称与 JavaBean 属性动态关联起来。当 JavaBean 属性使用 XmlElement 进行注释时,XML 元素名称以静态方式派生于 JavaBean 属性名称。但是,在使用此注释时,XML 元素名称派生于运行时的 JavaBean 属性类型的实例。

XML 模式替换组支持

XML 模式允许 XML 文档作者使用 XML 元素名称,这里的名称指没有在使用替换组的模式的内容模式中静态指定的名称。模式派生代码为使用元素属性 的替换组提供了支持(请参见 JAXB 2.0 规范的第 5.5.5 节:“元素属性”)。元素属性方法签名的形式如下:

public void setTerm(JAXBElement<? extends Operator?>);
public JAXBElement<? extends Operator?> getTerm();
 

XmlElementDecl 注释的元素工厂方法用于创建包含 XML 元素名称的 JAXBElement 实例。元素属性中出现 @XmlElementRef 注释则表示使用取自 JAXBElement 实例的元素名称,而不是从 JavaBean 属性名称中派生一个 XML 元素名称。

使用上受到以下约束的限制:

  • 如果 collection 项类型(用于 collection 属性)或属性类型(用于单个赋值属性)是 javax.xml.bind.JAXBElement,则 @XmlElementRef}.name()@XmlElementRef.namespace() 必须指向一个元素工厂方法,并在使用 @XmlRegistry 注释的类中存在一个 @XmlElementDecl 注释(通常 ObjectFactory 类由模式编译器生成):
    • @XmlElementDecl.name() 必须等于 @XmlElementRef.name()
    • @XmlElementDecl.namespace() 必须等于 @XmlElementRef.namespace()。
  • 如果 collection 项类型(用于 collection 属性)或属性类型(用于单个赋值属性)不是 javax.xml.bind.JAXBElement,则由属性或字段引用的类型必须是使用 XmlRootElement 注释的。
  • 此注释可与以下注释一起使用:XmlElementWrapperXmlJavaTypeAdapter

更多公共信息,请参见 javax.xml.bind.package javadoc 中的“包规范”。

示例 1:Ant 任务示例

以下 Java 类分层结构模拟 Ant 构建脚本。Ant 任务对应于类分层结构中的类。Ant 任务的 XML 元素名称由其对应类上的 @XmlRootElement 注释指示。
     @XmlRootElement(name="target")
     class Target {
         // The presence of @XmlElementRef indicates that the XML
         // element name will be derived from the @XmlRootElement 
         // annotation on the type (for e.g. "jar" for JarTask). 
         @XmlElementRef
         List<Task> tasks;
     }

     abstract class Task {
     }

     @XmlRootElement(name="jar")
     class JarTask extends Task {
         ...
     }

     @XmlRootElement(name="javac")
     class JavacTask extends Task {
         ...
     }

     <!-- XML Schema fragment -->
     <xs:element name="target" type="Target">
     <xs:complexType name="Target">
       <xs:sequence>
         <xs:choice maxOccurs="unbounded">
           <xs:element ref="jar">
           <xs:element ref="javac">
         </xs:choice>
       </xs:sequence>
     </xs:complexType>

 

因此下列代码片段

Target target = new Target();
target.tasks.add(new JarTask());
target.tasks.add(new JavacTask());
marshal(target);
 
将生成以下 XML 输出:

     &lt;target&gt;
       &lt;jar&gt;
         ....
       &lt;/jar&gt;
       &lt;javac&gt;
         ....
       &lt;/javac&gt;
     &lt;/target&gt;
 

有一个类扩展了不具有 XmlRootElementTask 并不是一个错误。但是这些类将无法出现在 XML 实例中(因为它们没有 XML 元素名称)。

示例 2:XML 模式替换组支持

以下示例显示用于 XML 模式替换组的注释。注释和 ObjectFactory 都派生于模式。

     @XmlElement
     class Math {
         //  The value of #type()is 
         //  JAXBElement.class , which indicates the XML
         //  element name ObjectFactory - in general a class marked
         //  with @XmlRegistry. (See ObjectFactory below)
         //  
         //  The #name() is "operator", a pointer to a
         // factory method annotated with a
         //  XmlElementDecl with the name "operator". Since
         //  "operator" is the head of a substitution group that
         //  contains elements "add" and "sub" elements, "operator"
         //  element can be substituted in an instance document by
         //  elements "add" or "sub". At runtime, JAXBElement
         //  instance contains the element name that has been
         //  substituted in the XML document.
         // 
         @XmlElementRef(type=JAXBElement.class,name="operator")
         JAXBElement<? extends Operator> term;
     }

     @XmlRegistry
     class ObjectFactory {
         @XmlElementDecl(name="operator")
         JAXBElement<Operator> createOperator(Operator o) {...}
         @XmlElementDecl(name="add",substitutionHeadName="operator")
         JAXBElement<Operator> createAdd(Operator o) {...}
         @XmlElementDecl(name="sub",substitutionHeadName="operator")
         JAXBElement<Operator> createSub(Operator o) {...}
     }

     class Operator {
         ...
     }
 

因此以下代码片段

Math m = new Math();
m.term = new ObjectFactory().createAdd(new Operator());
marshal(m);
 
将生成以下 XML 输出:
<math>
<add>...</add>
</math>
 
英文文档:

Maps a JavaBean property to a XML element derived from property's type.

Usage

@XmlElementRef annotation can be used with a JavaBean property or from within XmlElementRefs

This annotation dynamically associates an XML element name with the JavaBean property. When a JavaBean property is annotated with XmlElement, the XML element name is statically derived from the JavaBean property name. However, when this annotation is used, the XML element name is derived from the instance of the type of the JavaBean property at runtime.

XML Schema substitution group support

XML Schema allows a XML document author to use XML element names that were not statically specified in the content model of a schema using substitution groups. Schema derived code provides support for substitution groups using an element property, (section 5.5.5, "Element Property" of JAXB 2.0 specification). An element property method signature is of the form:

     public void setTerm(JAXBElement&lt;? extends Operator?&gt;);
     public JAXBElement&lt;? extends Operator?&gt; getTerm();
 

An element factory method annotated with XmlElementDecl is used to create a JAXBElement instance, containing an XML element name. The presence of @XmlElementRef annotation on an element property indicates that the element name from JAXBElement instance be used instead of deriving an XML element name from the JavaBean property name.

The usage is subject to the following constraints:

  • If the collection item type (for collection property) or property type (for single valued property) is JAXBElement, then @XmlElementRef}.name() and @XmlElementRef.namespace() must point an element factory method with an @XmlElementDecl annotation in a class annotated with @XmlRegistry (usually ObjectFactory class generated by the schema compiler) :
    • @XmlElementDecl.name() must equal @XmlElementRef.name()
    • @XmlElementDecl.namespace() must equal @XmlElementRef.namespace().
  • If the collection item type (for collection property) or property type (for single valued property) is not JAXBElement, then the type referenced by the property or field must be annotated with XmlRootElement.
  • This annotation can be used with the following annotations: XmlElementWrapper, XmlJavaTypeAdapter.

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

Example 1: Ant Task Example

The following Java class hierarchy models an Ant build script. An Ant task corresponds to a class in the class hierarchy. The XML element name of an Ant task is indicated by the @XmlRootElement annotation on its corresponding class.
     @XmlRootElement(name="target")
     class Target {
         // The presence of @XmlElementRef indicates that the XML
         // element name will be derived from the @XmlRootElement 
         // annotation on the type (for e.g. "jar" for JarTask). 
         @XmlElementRef
         List<Task> tasks;
     }

     abstract class Task {
     }

     @XmlRootElement(name="jar")
     class JarTask extends Task {
         ...
     }

     @XmlRootElement(name="javac")
     class JavacTask extends Task {
         ...
     }

     <!-- XML Schema fragment -->
     <xs:element name="target" type="Target">
     <xs:complexType name="Target">
       <xs:sequence>
         <xs:choice maxOccurs="unbounded">
           <xs:element ref="jar">
           <xs:element ref="javac">
         </xs:choice>
       </xs:sequence>
     </xs:complexType>

 

Thus the following code fragment:

     Target target = new Target();
     target.tasks.add(new JarTask());
     target.tasks.add(new JavacTask());
     marshal(target);
 
will produce the following XML output:

     &lt;target&gt;
       &lt;jar&gt;
         ....
       &lt;/jar&gt;
       &lt;javac&gt;
         ....
       &lt;/javac&gt;
     &lt;/target&gt;
 

It is not an error to have a class that extends Task that doesn't have XmlRootElement. But they can't show up in an XML instance (because they don't have XML element names).

Example 2: XML Schema Susbstitution group support

The following example shows the annotations for XML Schema substitution groups. The annotations and the ObjectFactory are derived from the schema.

     @XmlElement
     class Math {
         //  The value of type()is 
         //  JAXBElement.class , which indicates the XML
         //  element name ObjectFactory - in general a class marked
         //  with @XmlRegistry. (See ObjectFactory below)
         //  
         //  The name() is "operator", a pointer to a
         // factory method annotated with a
         //  XmlElementDecl with the name "operator". Since
         //  "operator" is the head of a substitution group that
         //  contains elements "add" and "sub" elements, "operator"
         //  element can be substituted in an instance document by
         //  elements "add" or "sub". At runtime, JAXBElement
         //  instance contains the element name that has been
         //  substituted in the XML document.
         // 
         @XmlElementRef(type=JAXBElement.class,name="operator")
         JAXBElement<? extends Operator> term;
     }

     @XmlRegistry
     class ObjectFactory {
         @XmlElementDecl(name="operator")
         JAXBElement<Operator> createOperator(Operator o) {...}
         @XmlElementDecl(name="add",substitutionHeadName="operator")
         JAXBElement<Operator> createAdd(Operator o) {...}
         @XmlElementDecl(name="sub",substitutionHeadName="operator")
         JAXBElement<Operator> createSub(Operator o) {...}
     }

     class Operator {
         ...
     }
 

Thus, the following code fragment

     Math m = new Math();
     m.term = new ObjectFactory().createAdd(new Operator());
     marshal(m);
 
will produce the following XML output:
     <math>
       <add>...</add>
     </math>
 

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

Optional Element Summary
 String
 String
 Class
 

abstract public Class<T> type()
被引用的 Java 类型。

如果该值为 DEFAULT.class,则可以从 JavaBean 属性类型推导出该类型。

英文文档:

type

public abstract Class type
The Java type being referenced.

If the value is DEFAULT.class, the type is inferred from the the type of the JavaBean property.

Default:
javax.xml.bind.annotation.XmlElementRef.DEFAULT.class

abstract public String namespace()
可以使用此参数和 #name() 来确定 JavaBean 属性的 XML 元素。

如果 type()JAXBElement.class,那么 namespace()name() 指向带有 XmlElementDecl 的工厂方法。XML 元素名称是工厂方法的 XmlElementDecl 注释的元素名称,如果取自其替换组中的元素(替换组的头元素)在 XML 文档中已被替换,则 XML 元素名称取自替换元素的 XmlElementDecl

如果 #type() 不是 JAXBElement.class,那么 XML 元素名称是与使用该类型上的 XmlRootElement 注释的类型静态关联的 XML 元素名称。如果没有使用 XmlElementDecl 注释该类型,那么它是一个错误。

如果 type() 不是 JAXBElement.class,那么此值必须是 ""。

英文文档:

namespace

public abstract String namespace
This parameter and name() are used to determine the XML element for the JavaBean property.

If type() is JAXBElement.class , then namespace() and name() point to a factory method with XmlElementDecl. The XML element name is the element name from the factory method's XmlElementDecl annotation or if an element from its substitution group (of which it is a head element) has been substituted in the XML document, then the element name is from the XmlElementDecl on the substituted element.

If type() is not JAXBElement.class, then the XML element name is the XML element name statically associated with the type using the annotation XmlRootElement on the type. If the type is not annotated with an XmlElementDecl, then it is an error.

If type() is not JAXBElement.class, then this value must be "".

Default:
""

abstract public String name()
See also namespace()
英文文档:

name

public abstract String name
See Also:
namespace()
Default:
"##default"


Submit a bug or feature

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

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

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