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. EXCEL 操作

    1.为几万行数据加序号 先在A1,A2分别输入1,2,选中A1:A2,双击A2右下角那个小方块. 数据有多少行就会自动填充多少行(要求:B列数据连续) 2.统计一列中单元格的值等于某个值的单元格的个数 ...

  2. [原创]git使用入门

    创建git项目并初始化 建立一个新文件夹,然后将该文件夹定义为git项目 Lilis-MacBook-Pro:GitDir lili$ mkdir testgit Lilis-MacBook-Pro: ...

  3. 访问修饰符(access modifier)。

    1)public(公共的):被 public所修饰的属性和方法可以被有类访问. 所修饰的属性和方法可以被有类访问. 2)protected (受保护的):被protected所修饰的属性和方法可以在类 ...

  4. poj1848 Tree

    .....是我多想了. 我想开f[][0~3],看到百度上的题解都是[0~2]的,我就改了 方程不是特别难想.. f代表最小代价 f[i][0]是子树有环过i f[i][1]是子树除了i都成环了 f[ ...

  5. linux产生静态库和动态库

    如何构建一个库 静态库(.a) 静态库的建立和使用是非常简单的: 1. 编译源文件:gcc -Wall -c test1.c test2.c 2. 生成库文件:ar -cvq libtest.a 3. ...

  6. Android实例-调用GOOGLE的TTS实现文字转语音(XE7+小米2)(无图)

    注意:在手机上必须选安装文字转语音引擎“google Text To Speech”地址:http://www.shouji56.com/soft/GoogleWenZiZhuanYuYinYinQi ...

  7. js不验证

    给select添加了id,人家默认就有个id,id冲突导致js不验证

  8. UIViewController生命周期测试

    push进入  -[NaviRootVC viewWillDisappear:]  -[NextVC viewWillAppear:]  -[NextVC viewWillLayoutSubviews ...

  9. C#将对象转换成JSON字符串,Newtonsoft.Json (JSON.NET)

    官方API说明文档 http://www.newtonsoft.com/json/help/html/N_Newtonsoft_Json.htm http://www.newtonsoft.com/ ...

  10. 常用 Unicode 符号列表

    Unicode 中包含了各种符号,我们可以作为图标来使用.这里整理一下常用的一些符号. 一.拉丁字母补全(Latin-1 Supplement):U+0080 至 U+00FF Unicode 编号 ...