2016-04-18 26 views
1

私はXMLスキーマファイルに基づいてJavaクラスを生成するためにgradleを使用しています。私は 'org.glassfish.jaxb:jaxb-xjc:2.2.11'と 'org.glassfish.jaxb:jaxb-runtime:2.2.11'を依存関係として使用していますので、 'com.sun.tools.xjc'を使用できます。 XJC2Task 'クラスを生成するクラスです。生成されたJAXBクラスのXmlTypeを変更するには

これは、スキーマファイルである:生成されたクラスの

<?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注釈に名前値を変更できますか?私はbindingsfileを使用してみましたし、bindingsfileではなく、運なし<javaType>タグを実験しようとしていることが

@XmlType(name = "record") 

ようにしたいと思います。

EDIT: 私はこれを変更する必要がある理由は、私はキャメル(「JAXBとのStAXを使用したコレクションの反復」と呼ばれるhttp://camel.apache.org/stax.htmlセクション)からSTAXスプリッタを使用してXMLファイルを分割する必要があるということです。 これは、@XmlTypeアノテーションのname属性を調べて、ファイル内で分割するxmlタグを認識します。認識されたタグ(<record>)は、JAXBがRecordType Javaクラスに解析されます。

答えて

0

@XmlTypeアノテーションの名前は、スキーマファイル内のcomplexTypeの名前です。これは、この注釈に対してパラメータ 'name'が定義されている方法です。

あなたはそれを変更したいのであれば、あなたはあなたのスキーマでのcomplexTypeの名前を変更する必要があります。

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

私は、スキーマファイルを変更できないことを言及するのを忘れてしまいました。なぜ私がこれを必要としているかについての質問に、いくつかの情報を追加しました。 – npeder

関連する問題