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.

@XmlAccessorType( XmlAccessType.PUBLIC_MEMBER )
public class SomeClass {
private String a;
private String b; public SomeClass(){ ... } public String getA(){ ... }
public void setA( String value ){ ... } @XmlTransient
public String getB(){ ... }
public void setB( String value ){ ... }
}

The corresponding XML schema type definition looks like this:

<xs:complexType name="someClass">
<xs:sequence>
<xs:element name="a" type="xs:string" minOccurs="0"/>
</xs:sequence>
</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.

@XmlAccessorType( XmlAccessType.NONE )
public class OtherClass {
private String a;
private String b; public OtherClass(){ ... } public String getA(){ ... }
public void setA( String value ){ ... } @XmlElement( required = true )
public String getB(){ ... }
public void setB( String value ){ ... }
}

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

<xs:complexType name="otherClass">
<xs:sequence>
<xs:element name="b" type="xs:string"/>
</xs:sequence>
</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.)

@XmlAccessorType( XmlAccessType.PUBLIC_MEMBER )
public class SpecialClass {
@XmlTransient
public String a;
private List<String> b; public SpecialClass(){ ... } public String getA(){ ... }
public void setA( String value ){ ... } @XmlElement
public List<String> getB(){
if( b == null ) b = new ArrayList<String>();
return b;
}
}

The generated complex type features both elements.

<xs:complexType name="specialClass">
<xs:sequence>
<xs:element name="a" type="xs:string" minOccurs="0"/>
<xs:element name="b" type="xs:string" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</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. 阿里云centos 安装 nodejs npm express

    yum check-update yum install vsftpdvi /etc/vsftpd/vsftpd.conf anonymous_enable=NO service vsftpd sta ...

  2. C++&OpenCV中读取灰度图像到数组的两种

    如标题所言,此处是对于灰度图像而言 ///method 1 read the image data one by one for (int row = 0, i = 0;row < imgDst ...

  3. 1N系列稳压二极管参数

    1N系列稳压二极管参数 型号 稳定电压 型号 稳定电压 型号 稳定电压 1N5236 7.5 1N5738 12 1N6002 12 1N5237 8.2 1N5739 13 1N6003 13 1N ...

  4. iOS 详解NSXMLParser方法解析XML数据方法

    前一篇文章已经介绍了如何通过URL从网络上获取xml数据.下面介绍如何将获取到的数据进行解析. 下面先看看xml的数据格式吧! <?xml version="1.0" enc ...

  5. 射频识别技术漫谈(15)——Mifare1的安全性及7字节序列号M1卡【worlsing笔记】

    Mifare1的安全性主要指卡中数据的安全性,要求卡中的数据不能被非法修改或窃听.数据的安全性主要使用加密技术来保证,加密技术有两个关键因素:加密算法和密钥.现代加密技术的一大特点是加密算法公开,如果 ...

  6. 射频识别技术漫谈(9)——动物标签HDX【worldsing笔记】

    半双工(HDX,Half Duplex)技术是ISO11784/11785中规定的另一种标签与读写器之间的通讯方式.读写器先打开射频场对标签充电以激活标签,然后关闭磁场,标签在读写器磁场关闭的情况下向 ...

  7. express源码剖析2

    当使用express时,代码会这样写: var express = require('express'); 如果创建一个express的应用,代码会这样写: var app = express(); ...

  8. iOS 分类和继承

    iOS 中分类(Categories) 和 继承(Inherit)有相同的功能,但在一些细节上又有差异,简单介绍一下两者的异同. 分类可以在不知道系统类源代码的情况下,为这个类添加新的方法.分类只能用 ...

  9. iOS OC语言: Block底层实现原理 (转载)

    作者:Liwjing 地址:http://www.jianshu.com/users/8df89a9d8380/latest_articles 先来简单介绍一下Block Block是什么? 苹果推荐 ...

  10. Riesz-Thorin插值不等式