【发布时间】:2016-08-10 02:18:45
【问题描述】:
我正在使用 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")
我曾尝试使用绑定文件并尝试使用绑定文件中的<javaType> 标记,但没有运气。
编辑:
我需要更改它的原因是我需要使用 Camel 的 stax 拆分器拆分 XML 文件(http://camel.apache.org/stax.html 部分称为“使用 JAXB 和 StAX 遍历集合”)。
这会查看 @XmlType 注释的名称属性,以识别要在文件中拆分的 xml 标记。识别的标签 (<record>) 将被 JAXB 解析为 RecordType java 类。
【问题讨论】: