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. Spring配置MyBatis

    1.MyBatis配置文件(mybatis-config) <?xml version="1.0" encoding="UTF-8"?> <! ...

  2. zoj 1622 Switch 开关灯 简单枚举

    ZOJ Problem Set - 1622 Switch Time Limit: 2 Seconds      Memory Limit: 65536 KB There are N lights i ...

  3. PowerDesigner 业务处理模型( BPM )

    PowerDesigner 业务处理模型( BPM ) 说明 properties语言文档xmlvalidation数据库   目录(?)[+]   一.     BPM 简介 业务处理模型(Busi ...

  4. SQLite使用教程9 Select 语句

    http://www.runoob.com/sqlite/sqlite-select.html SQLite Select 语句 SQLite 的 SELECT 语句用于从 SQLite 数据库表中获 ...

  5. SpringMVC学习 DispatcherServlet (转载)

    3.1.DispatcherServlet作用 DispatcherServlet是前端控制器设计模式的实现,提供spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring Io ...

  6. 最牛X的编码套路

    最近,我大量阅读了Steve Yegge的文章.其中有一篇叫"Practicing Programming"(练习编程),写成于2005年,读后令我惊讶不已: 与你所相信的恰恰相反 ...

  7. Asp.Net Core- 多样性的配置来源

    我们知道,ConfigurationProvider提供将数据源转换为字典的功能,数据源可以分为很多种,比如:物理文件.数据库.内存变量等等.物理文件又包括很多种类型的文件,比如:xml.json等等 ...

  8. 将java源码打成jar包

    方法一:通过jar命令 jar命令的用法: 下面是jar命令的帮助说明: 用法:jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] ...

  9. 关于python文件操作 (转载)

    总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧: python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Pyth ...

  10. 如何使用Linux通用后门(转zafe)

    特别提示:仅用于安全测试和教学,禁止非法用途. 标题党了,呵呵 其实就是个ssh后门,基本可以不用看内核版本,很简单,为照顾新手! ********************************** ...