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. 把方法转json在前台输出

    在JSON中,有两种结构:对象和数组. 1. 一个对象以“{”(左括号)开始,“}”(右括号)结束.每个“名称”后跟一个“:”(冒号):“‘名称/值’ 对”之间运用 “,”(逗号)分隔. 名称用引号括 ...

  2. IP查找工具——angry IP Scanner

    作用] 若树莓派没有屏幕可通过该工具查找树莓派的IP地址.例如下图的树莓派IP地址可能为192.168.1.107. 图1 IP scan查找树莓派IP地址

  3. Exercise: Rot13 Reader

    package main import ( "io" "os" "strings" "fmt" ) type rot13 ...

  4. [iOS基础控件 - 6.6] 展示团购数据 自定义TableViewCell

    A.需求 1.头部广告 2.自定义cell:含有图片.名称.购买数量.价格 3.使用xib设计自定义cell,自定义cell继承自UITableViewCell 4.尾部“加载更多按钮”,以及其被点击 ...

  5. RS485连接CAN——应急用法【worldsing笔记】【待完善】

    阅读前提:假设读者对CAN总线和485总线有一定了解. RX485连接CAN用法提出背景: 在一般情况下只能是CAN对CAN 485对485, 但是在调试过程中难免对出现设备没有CAN接口,或是没有4 ...

  6. web.xml(spring/spring mvc/hibernate)

    <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" ...

  7. Java任务调度

    最近项目要用到任务调度的相关知识,昨天信心满满的去官网学习,结果被坑个半死,我用的最新版的quartz,文档里说是兼容所有版本,但是代码连编译都报错,无奈只好从网上找资料,摸着石头过河总算有点眉目,在 ...

  8. 【STL学习】智能指针之weak_ptr

    简介 weak_ptr是shared_ptr的观察者,它不会干扰shared_ptr所共享对象的所有权,当一个weak_ptr所观察的shared_ptr要释放它的资源时,它会把相关的weak_ptr ...

  9. Float(浮动)

    一.什么是浮动? CSS 的 Float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列. 二.浮动元素的特点 元素浮动后会自动变成行块元素 浮动元素的父元素高度宽计算将忽略浮动子元素 浮动 ...

  10. android studio简易了解第一部分

    1.如果还没下载 jdk,先把jdk下载,然后下载android studio 安装 百度 android studio ,百度软件中心可以下载. Android官网可以下载:     网址: htt ...