If JAXB binds a class to XML, then, by default, all public members will be bound, i.e., public getter and setter pairs, or public fields. Any protected, package-visible or private member is bound if it is annotated with a suitable annotation such as XmlElement or XmlAttribute. You have several possibilities to influence this default behaviour.

You can annotate a package or a top level class with XmlAccessorType, setting its value element to one of the enum constants FIELDPROPERTYPUBLIC_MEMBER or NONE. If FIELD is set every non static, non transient field will be automatically bound. PROPERTY instructs JAXB to do this for getter and setter pairs. NONE suppresses bind except for explicitly annotated fields or properties. A class without this annotation inherits the XmlAccessorType setting either from its superclass or from the package setting.

The other annotation to be mentioned in this context is XmlTransient. It suppresses binding for its target which can be an entire class or a field or a method. This is also useful if you have a name clash resulting from a public field, say fooand methods getFoo and setFoo.

The first class illustrates a class that restricts the set of XML elements from an accessor type setting of PUBLIC_MEMBER. Member getB is blocked from being bound.

  1. @XmlAccessorType( XmlAccessType.PUBLIC_MEMBER )
  2. public class SomeClass {
  3. private String a;
  4. private String b;
  5.  
  6. public SomeClass(){ ... }
  7.  
  8. public String getA(){ ... }
  9. public void setA( String value ){ ... }
  10.  
  11. @XmlTransient
  12. public String getB(){ ... }
  13. public void setB( String value ){ ... }
  14. }

The corresponding XML schema type definition looks like this:

  1. <xs:complexType name="someClass">
  2. <xs:sequence>
  3. <xs:element name="a" type="xs:string" minOccurs="0"/>
  4. </xs:sequence>
  5. </xs:complexType>

The second example illustrates the reverse process. It shows a class with the most restrictive accessor type setting, with one member explicitly annotated as an element.

  1. @XmlAccessorType( XmlAccessType.NONE )
  2. public class OtherClass {
  3. private String a;
  4. private String b;
  5.  
  6. public OtherClass(){ ... }
  7.  
  8. public String getA(){ ... }
  9. public void setA( String value ){ ... }
  10.  
  11. @XmlElement( required = true )
  12. public String getB(){ ... }
  13. public void setB( String value ){ ... }
  14. }

Since we have set the annotation element required to true, the generated schema snippet is slightly different:

  1. <xs:complexType name="otherClass">
  2. <xs:sequence>
  3. <xs:element name="b" type="xs:string"/>
  4. </xs:sequence>
  5. </xs:complexType>

The final example for this topic demonstrates using these annotations in somewhat special circumstances. First, XmlTransient is used on the public field to avoid the name clash with the method pair. Second, XmlElement is used to request binding for getB, which doesn't have its setB spouse. (The getter follows the standard pattern of the JAXB generated Java code for elements bound to List<?>, with changes being made on the list object.)

  1. @XmlAccessorType( XmlAccessType.PUBLIC_MEMBER )
  2. public class SpecialClass {
  3. @XmlTransient
  4. public String a;
  5. private List<String> b;
  6.  
  7. public SpecialClass(){ ... }
  8.  
  9. public String getA(){ ... }
  10. public void setA( String value ){ ... }
  11.  
  12. @XmlElement
  13. public List<String> getB(){
  14. if( b == null ) b = new ArrayList<String>();
  15. return b;
  16. }
  17. }

The generated complex type features both elements.

  1. <xs:complexType name="specialClass">
  2. <xs:sequence>
  3. <xs:element name="a" type="xs:string" minOccurs="0"/>
  4. <xs:element name="b" type="xs:string" maxOccurs="unbounded" minOccurs="0"/>
  5. </xs:sequence>
  6. </xs:complexType>

Taken together, this means that you can, either at package level or at some superclass, define the strategy for all classes within the package or for all subclasses, respectively. This strategy may be generally permissive, oriented on fields or properties, or restrictive, permitting nothing by default. Within a class, you may extend a restrictive setting by adding XmlElement or XmlAttribute, or you may inhibit bindings using the XmlTransient annotation.

JAXB - Annotations, Controlling Element Selection: XmlAccessorType, XmlTransient的更多相关文章

  1. JAXB - Annotations, The Annotation XmlElement

    The basic annotation for a field that's intended to be an element is XmlElement. It permits you to d ...

  2. JAXB - Annotations, Top-level Elements: XmlRootElement

    A class that describes an XML element that is to be a top-level element, i.e., one that can function ...

  3. JAXB - Annotations, Annotations for the Schema: XmlSchema

    This annotation can only be used with a package. It defines parameters that are derived from the xsd ...

  4. JAXB - Annotations, The Object Factory: XmlRegistry, XmlElementDecl

    To be able to create objects from XML elements, the unmarshaller must have an object factory with me ...

  5. JAXB - Annotations, Class Fields as Attributes: XmlAttribute

    Provided that XML lets you represent a data item as a single value, there is no cut-and-dried rule f ...

  6. JAXB - Annotations, Type Mapping: XmlSchemaType

    The annotation XmlSchemaType defines a mapping between an arbitrary Java type and a simple schema bu ...

  7. JAXB - Annotations, Annotation for Classes: XmlType

    This annotation adds information that would be available from a schema type, but isn't implied by a ...

  8. JAXB - Annotations, Annotations for Enums: XmlEnum, XmlEnumValue

    An enum type is annotated with XmlEnum. It has an optional element value of type java.lang.Class whi ...

  9. JAXB - Annotations, Type Adapters: XmlJavaTypeAdapter

    For some Java container types JAXB has no built-in mapping to an XML structure. Also, you may want t ...

随机推荐

  1. 【bzoj2440】完全平方数

    题意: 求第n个不为完全平方数倍数的数 题解: 网上有人说答案不会超过2n (求证0 0?) 竟然不超过2n 那么很明显就是用二分做了 二分判定就是要求小于等于n的合法的数的个数 不难发现一个数若为完 ...

  2. Java 8 开发顶级技巧

    本文由码农网 – 小峰原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 我使用Java 8编码已经有些年头,既用于新的应用程序,也用来迁移现有的应用,感觉是时候写一些我发现的非常有用的 ...

  3. Oracle-数据实现竖排打印

    --存放重证评分的数据表create table ZZPFapache2(  ZZ_datetime DATE,   --时间  ZZ_zongfen INTEGER, --总分  ZZ_shiwan ...

  4. hdu 5533 Dancing Stars on Me

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5533 Dancing Stars on Me Time Limit: 2000/1000 MS (Ja ...

  5. 一个通用数据库访问类(C#,SqlClient)

    本文转自:http://www.7139.com/jsxy/cxsj/c/200607/114291.html使用ADO.NET时,每次数据库操作都要设置connection属性.建立connecti ...

  6. CGAffineTransform相关函数

    CoreGraphics.h CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2);
[xxx setTransform ...

  7. cas 3.5.2 登录成功后,如何返回用户更多信息?

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  8. python会什么比c慢

    众所周知,python执行速度比c慢.原因为何? 先来看下面这张图: python的传统运行执行模式:录入的源代码转换为字节码,之后字节码在python虚拟机中运行.代码自动被编译,之后再解释成机器码 ...

  9. MST最小生成树及克鲁斯卡尔(Kruskal)算法

    最小生成树MST,英文名如何拼写已忘,应该是min spaning tree吧.假设一个无向连通图有n个节点,那么它的生成树就是包括这n个节点的无环连通图,无环即形成树.最小生成树是对边上权重的考虑, ...

  10. android的进度条使用

    android的进度条 1.实现的效果 2.布局代码 先写一个my_browser.xml文件 存放WebView <?xml version="1.0" encoding= ...