【问题标题】:How to change XmlType of generated JAXB classes如何更改生成的 JAXB 类的 XmlType
【发布时间】: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")

我曾尝试使用绑定文件并尝试使用绑定文件中的&lt;javaType&gt; 标记,但没有运气。

编辑: 我需要更改它的原因是我需要使用 Camel 的 stax 拆分器拆分 XML 文件(http://camel.apache.org/stax.html 部分称为“使用 JAXB 和 StAX 遍历集合”)。 这会查看 @XmlType 注释的名称属性,以识别要在文件中拆分的 xml 标记。识别的标签 (&lt;record&gt;) 将被 JAXB 解析为 RecordType java 类。

【问题讨论】:

    标签: xsd jaxb jaxb2 xjc


    【解决方案1】:

    您可以使用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>
    

    我不知道这是否应该被视为一种黑客行为,但它确实有效。有趣的是,在我的例子中,即使是 XmlTypenamespace 属性仍然会生成并填充与不添加显式注释时相同的值。

    【讨论】:

      【解决方案2】:

      @XmlType 注释中的名称是架构文件中复杂类型的名称。这就是为此注解定义参数“名称”的方式。

      所以,如果你想改变它,你必须在你的架构中改变 complexType 的名字:

      <xs:complexType name="record">
          <xs:attribute name="key" type="xs:string"/>
          <xs:attribute name="value" type="xs:string"/>
      </xs:complexType>
      

      【讨论】:

      • 我忘了提到我无法更改架构文件。在关于我为什么需要这个的问题中添加了更多信息。
      猜你喜欢
      • 2012-01-08
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      相关资源
      最近更新 更多