2012-01-02 5 views
3

私は、JAXBとintellij webservicesプラグインを使用してXSDからJavaファイルを作成しています。私は同じオブジェクトを定義する2つのXSDを持っていますが、 "XMLスキーマからJavaコードを生成"を使用してそれらを作成すると、オブジェクトは自分のパッケージで2回作成されます。私はすでにimport xsdでref属性を使ってみましたが、同じ結果が得られます。ここで同じカスタムオブジェクトを使用するために2つのXSDを作成する方法

は一例です。

これは

<?xml version="1.0" encoding="UTF-8"?> 
    <xs:schema targetNamespace="http://www.msp-gs.com/workflow" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
     xmlns:wc="http://www.example.com/workflow" 
     attributeFormDefault="unqualified" 
     elementFormDefault="qualified" 
     jaxb:version="1.0"> 

    <xs:annotation> 
    <xs:appinfo> 
     <jaxb:globalBindings enableJavaNamingConventions="true"> 

     </jaxb:globalBindings> 
    </xs:appinfo> 
    </xs:annotation> 

    <xs:element name="WC"> 

    <xs:complexType> 

     <xs:sequence> 

      <xs:element name="Example" 
         type="wc:Restriction" 
         minOccurs="1" 
         maxOccurs="unbounded"> 

      </xs:element> 

     </xs:sequence> 

     </xs:complexType> 

    </xs:element> 

    <xs:complexType name="Restriction"> 

     <xs:attribute type="xs:string" 
        name="authorizationTreeId"/> 

    </xs:complexType> 

    </xs:schema> 

最初のXSDですこれは、私はその制限が同じオブジェクトになりたい二XSD

<?xml version="1.0" encoding="UTF-8"?> 
    <xs:schema targetNamespace="http://www.msp-gs.com/workflow" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
     xmlns:fd="http://www.example.com/workflow" 
     attributeFormDefault="unqualified" 
     elementFormDefault="qualified" 
     jaxb:version="1.0"> 

     <xs:annotation> 
     <xs:appinfo> 
     <jaxb:globalBindings enableJavaNamingConventions="true"> 

     </jaxb:globalBindings> 
    </xs:appinfo> 
     </xs:annotation> 

    <xs:element name="FD"> 

     <xs:complexType> 

     <xs:sequence> 

      <xs:element name="Example" 
         type="fd:Restriction" 
         minOccurs="1" 
         maxOccurs="unbounded"> 

      </xs:element> 

     </xs:sequence> 

    </xs:complexType> 

    </xs:element> 

    <xs:complexType name="Restriction"> 

    <xs:attribute type="xs:string" 
        name="authorizationTreeId"/> 

    </xs:complexType> 

    </xs:schema> 

です。

ありがとうございます。

+1

チェックアウト次の記事:http://blog.bdoughan.com/2011/12/reusing-generated-jaxb- classes.html –

+1

ありがとうございます、私はすぐにそれに取り組み、それが動作しているかどうかをお知らせします。ありがとうございました。 – Rotem

+0

@Rotem:どちらの場合でも1つのXSDファイルを使用するのを止めるのはなぜですか? 2つのXSDの問題は、モデルオブジェクトを2回生成する必要がありますが、同じ出力ディレクトリを使用することができるため、2回目の実行で1が優先されます。または、スキーマの生成(コピー/クリーン)後にいくつかのトリックを実行できます。 –

答えて

3

JAXBには、次のような外部バインディングファイルを使用してJAXBを生成する代わりに、既存のJavaクラスを使用するように指定できます。例では、我々は既存のProductクラスを使用するJAXBを言っているの下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<bindings version="2.1" xmlns="http://java.sun.com/xml/ns/jaxb"> 
    <bindings scd="x-schema::tns" 
    xmlns:tns="http://www.example.org/Product"> 
    <schemaBindings map="false"/> 
    <bindings scd="tns:product"> 
     <class ref="org.example.product.Product"/> 
    </bindings> 
    </bindings> 
</bindings> 

を使用すると、XMLスキーマからクラスを生成するためにXJCツールを使用している場合は、XJCバインディングファイルを生成させる-episodeフラグを使用することができますそれが生成したすべてのクラスを指しています。これにより、以前に生成されたクラスを再利用することができます。詳細情報

xjc -d out -episode product.episode Product.xsd 

+0

antタスクを使用している場合は、次のようにepisode引数を使用できます。 Rotem

関連する問題