2

我正在使用 XML 模式和 xjc 来生成 java 类。我想定义一个如下所示的 XML 结构:

<unicorn color="white" superpower="transmogrification">Sparklemallow</unicorn>

具体来说,它具有属性和文本节点。我可以这样定义:

<xs:complexType name="unicorn">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="color" type="xs:string"/>
      <xs:attribute name="superpower" type="xs:string"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

当我生成一个Java类时,value默认调用文本节点所代表的属性:

public class Unicorn implements Serializable
{
    protected String value; //want to rename this
    protected String color;
    protected String superpower;

    ...

    public void setValue(String value) {
        this.value = value;
    }

    public boolean isSetValue() {
        return (this.value!= null);
    }
}

我想将文本节点属性重命名为语义上更合适的名称——在本例中为name. 有没有办法指定这个属性的名称应该是什么?

4

1 回答 1

3

我可以通过在 xs:complexType 节点下添加注释来更改此属性的名称:

<xs:complexType name="unicorn">
  <xs:annotation>
    <xs:appinfo>
      <jaxb:property name="name"/> <!-- your property name here -->
    </xs:appinfo>
  </xs:annotation>
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="color" type="xs:ID"/>
      <xs:attribute name="superpower" type="xs:string"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
于 2013-04-23T20:19:47.397 回答