我正在使用 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
。有没有办法指定此属性的名称?
最佳答案
我能够通过在 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>
关于java - XSD:指定文本节点属性的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16178445/