我使用gradle基于XML Schema文件生成Java类。我正在使用' org.glassfish.jaxb:jaxb-xjc:2.2.11'和' org.glassfish.jaxb:jaxb-runtime:2.2.11'作为依赖,所以我可以使用' com.sun.tools.xjc.XJC2Task'用于生成类的类。
这是架构文件:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="test"
targetNamespace="urn:oio:records:1.0.0"
xmlns="urn:oio:records:1.0.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="records" type="recordsType"/>
<xs:element name="record" type="recordType"/>
<xs:complexType name="recordsType">
<xs:sequence>
<xs:element ref="record" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="recordType">
<xs:attribute name="key" type="xs:string"/>
<xs:attribute name="value" type="xs:string"/>
</xs:complexType>
</xs:schema>
其中一个生成的类看起来像这样:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "recordType")
public class RecordType {
@XmlAttribute(name = "key")
protected String key;
@XmlAttribute(name = "value")
protected String value;
public String getKey() {return key;}
public void setKey(String value) {this.key = value;}
public String getValue() {return value;}
public void setValue(String value) {this.value = value;}
}
如何更改@XmlType注释中的名称值?我希望它是
@XmlType(name = "record")
我尝试过使用bindingsfile并试图在bindingsfile中试验<javaType>
标记,但没有运气。
修改
我需要更改这个的原因是我需要使用来自Camel的{stax splitter - (http://camel.apache.org/stax.html部分名为&#34;使用JAXB和StAX&#34;迭代一个集合)来拆分XML文件。
这将查看@XmlType
注释的name属性,以识别要在文件中拆分的xml标记。然后,识别的标记(<record>
)将被JAXB解析为RecordType java类。
答案 0 :(得分:0)
@XmlType注释中的名称是架构文件中complexType的名称。这就是参数&#39; name&#39;是为此注释定义的。
因此,如果要更改它,则必须更改架构中complexType的名称:
<xs:complexType name="record">
<xs:attribute name="key" type="xs:string"/>
<xs:attribute name="value" type="xs:string"/>
</xs:complexType>
答案 1 :(得分:0)
您可以使用jaxb2-annotate-plugin覆盖生成的Java类中name
属性的值。
对于您的代码,它看起来像这样:
<xs:schema id="test"
targetNamespace="urn:oio:records:1.0.0"
xmlns="urn:oio:records:1.0.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:annox="http://annox.dev.java.net"
jaxb:extensionBindingPrefixes="annox"
elementFormDefault="qualified">
<xs:element name="records" type="recordsType"/>
<xs:element name="record" type="recordType"/>
<xs:complexType name="recordsType">
<xs:sequence>
<xs:element ref="record" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="recordType">
<xs:annotation>
<xs:appinfo>
<annox:annotate target="class">
@javax.xml.bind.annotation.XmlType(name = "record")
</annox:annotate>
</xs:appinfo>
</xs:annotation>
<xs:attribute name="key" type="xs:string"/>
<xs:attribute name="value" type="xs:string"/>
</xs:complexType>
</xs:schema>
我不知道这是否应该被认为是hack,但是它可以解决问题。有趣的是,在我的情况下,即使namespace
的{{1}}属性仍然会生成并填充为与不添加该显式注释相同的值。