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

    1.二分查找 public class BubbleSort { public static int binarySerach(int[] a,int value){ int low=0; int h ...

  2. SQL 索引

    1.http://www.cnblogs.com/AK2012/archive/2013/01/04/2844283.html 2 .聚簇索引和非聚簇索引的区别 3.聚集索引:只能有一个  (相当于字 ...

  3. After Android Studio update: Gradle DSL method not found: 'runProguard()'

    1 具体报错为: Error:(16, 0) Gradle DSL method not found: 'runProguard()' Possible causes:<ul><li ...

  4. 关于AS3里的Matrix3D中的appendXXX和prependXXX

    最近在看3D相关的一些基础,因为搞as3这么多年了,决定3D基础这块还是从AS3入手,3D游戏开发这块从U3D入手,扯远了,研究Matrix3D类时发现了矩阵处理转换时的一些方法均分为appendXX ...

  5. ECshop数据库的访问统计和管理员日志的清空

    ECshop是个不错的系统,但是它有一定漏洞,若是访问量巨大的话,大量的访问统计代码会存入数据库的ecs_stats表中,甚至几天就可以达到几百兆,严重的网站直接就崩溃了.数据备份的时候也有很多不便, ...

  6. SpringMVC 参考文档

    原文地址:http://blog.csdn.net/lufeng20/article/details/7598801

  7. 【转】Android studio 导入github工程

    http://blog.csdn.net/feixiaku/article/details/45155587/ 从github下载两个开源项目: PagerSlidingTabStrip    |   ...

  8. 设置UITabBarController的背景颜色

    if (IOS7) { self.tabBarController.tabBar.barTintColor = kTAB_BAR_GB_COLOR; }else{ self.tabBarControl ...

  9. [React Fundamentals] Using Refs to Access Components

    When you are using React components you need to be able to access specific references to individual ...

  10. C 高级编程5 IO与文件权限

    三.基于文件的描术符号 .得到文件描术符号/释入文件描术符号 a.文件类型 目录文件 d 普通文件 f 字符设务文件 c 块设备文件 b 软连接文件 l 管道文件 p socket文件 s 字符设备文 ...