Provided that XML lets you represent a data item as a single value, there is no cut-and-dried rule for deciding between using an element or an attribute. (If you look for guidance, the schema describing the XML Schema language itself, making judicious use of both, is a good example.) JAXB, of course, has to be told when to make a field into an XML attribute.

The annotation for creating an XML attribute is XmlAttribute. Its elements correspond to what can be defined in an XML schema:

  • name defines the namestring for the attribute, the default being the class field's name.
  • namespace specifies the XML target namespace to be used for the attribute's name.
  • A "true" value of required is the same as using the XML Schema definition's attribute use="required".

If you ask about some way for defining the equivalent for the XML Schema attribute default="value", then the simple answer is: "Do it yourself." Just write the getter so that it returns the default value if the field's value is null.

It's possible to annotate a static final field with XmlAttribute. This has the same effect as an XML Schema definition where the attribute element's attribute fixed is set to that value.

@XmlAttribute
final static int answer = 42;

JAXB - Annotations, Class Fields as Attributes: XmlAttribute的更多相关文章

  1. JAXB - Annotations, Controlling Element Selection: XmlAccessorType, XmlTransient

    If JAXB binds a class to XML, then, by default, all public members will be bound, i.e., public gette ...

  2. 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 ...

  3. JAXB - Annotations, Annotation for Classes: XmlType

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

  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, Type Adapters: XmlJavaTypeAdapter

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

  6. 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 ...

  7. 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 ...

  8. JAXB - Annotations, Type Mapping: XmlSchemaType

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

  9. 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 ...

随机推荐

  1. Tcp服务端判断客户端是否断开连接

    今天搞tcp链接弄了一天,前面创建socket,绑定,监听等主要分清自己的参数,udp还是tcp的.好不容易调通了,然后就是一个需求,当客户端主动断开连接时,服务端也要断开连接,这样一下次客户端请求链 ...

  2. HIbernate学习笔记(七) hibernate中的集合映射和继承映射

    九.       集合映射 1. Set 2. List a)        @OrderBy 注意:List与Set注解是一样的,就是把Set更改为List就可以了 private List< ...

  3. 在多线程中进行UI操作

    那么在子线程中的UI操作如何处理呢?有两种方法: 一:在子线程,你需要进行的UI操作前添加dispatch_async函数,即可将代码块中的工作转回到主线程 dispatch_async(dispat ...

  4. 转载 asp.net的Request.ServerVariables参数说明

    转载原地址: http://blog.csdn.net/vincent_void/article/details/7739338 当讨论Request对象内容时,要研究的集合之一就是ServerVar ...

  5. 详解Windows平台搭建Androiod开发环境

    http://blog.csdn.net/lyq8479/article/details/6348330 1.安装JDK 2.安装SDK管理器,安装SDK(在线.离线) 3.下载安装Eclipse 4 ...

  6. CentOS下用Tomcat+Zookeeper+Nginx+Solr完美搭建SolrCloud平台(五)

    六.修改 /etc/rc.d/rc.local 文件,设置开机自启动 1.nginx 主机的设置 [root@nginx 桌面]# vi /etc/rc.d/rc.local #!/bin/sh to ...

  7. The Sorrows of Young Werther

    The Sorrows of Young Werther J.W. von Goethe Thomas Carlyle and R.D. Boylan Edited by Nathen Haskell ...

  8. cocos2d-x 添加纹理自动回收机制

    转自:http://www.cnblogs.com/lancidie/archive/2013/04/13/3019375.html 1.不是一个完整的模块,所以不提供完整代码,只提供思路和核心代码. ...

  9. iOS 有关自动轮播图片

    //初始化当前视图 _currentImageView = [[UIImageView alloc] init]; [_currentImageView setImageWithURL:[NSURL ...

  10. Start-Process传递变量

    如果$b="aa,bb" Start-Process PowerShell.exe -Argumentlist "d:\w.ps1 $a $b $c" Star ...