2012-01-20 40 views
3

を生成しません:JAXBはXSDスキーマが同じ型を持つ2つの異なる要素が含まれている同じタイプの異なる要素のためのクラス

<element name="subscriber" type="ns1:CreateSubscriberType"/> 
<element name="systemSubscriber" type="ns1:CreateSubscriberType"/> 

<complexType name="CreateSubscriberType"> 
<annotation> 
    <documentation>bla bla bla</documentation> 
</annotation> 
<sequence> 
    <element name="Header" type="ns2:DocumentHeader"/> 
    <element name="SubscriberDefault" type="ns2:SubscriberDefaultType"> 
    <annotation> 
     <documentation>bla bla</documentation> 
    </annotation> 
    </element> 
</sequence> 
</complexType> 

XSDスキーマが同じ型を持つ2つの異なる要素が含まれています。 それから私は、 maven-jaxb2-pluginを使ってこのxsdからクラスを生成しようとします。再配置はありません。クラスは生成されませんでした。要素の1つのタイプを変更すると、うまく動作し、2つのクラスが生成されます。私は公式の文書で説明を見つけられませんでした。誰もこのような問題に遭遇し、どのように修正することができますか?

+1

"resutがありません"のようなフレーズは使用しないでください。それが何をしているのかを教えてください。それから代わりに何をしたいのかを説明してください。 – skaffman

+0

mavenプラグインは、各要素に別々のクラスを生成する必要があります。 – Mirian

+0

いいえ、それは真実ではなく、それぞれのタイプのクラスを生成します。 – skaffman

答えて

3

JAXB(JSR-222)の実装では、それぞれの複合型のクラスが生成されます。このクラスのインスタンスは、その型の属性/要素に対応する任意のフィールド/プロパティで設定できるため、これは良好です。名前付き複合型の場合、それらを参照するグローバル要素は、ObjectFactoryクラスのメタデータとして取得されます。

schema.xsd以下

は、あなたのXMLスキーマのやや単純化したバージョンです:

<?xml version="1.0" encoding="UTF-8"?> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org" 
    xmlns:ns1="http://www.example.org" elementFormDefault="qualified"> 

    <element name="subscriber" type="ns1:CreateSubscriberType" /> 
    <element name="systemSubscriber" type="ns1:CreateSubscriberType" /> 

    <complexType name="CreateSubscriberType"> 
     <annotation> 
      <documentation>bla bla bla</documentation> 
     </annotation> 
     <sequence/> 
    </complexType> 

</schema> 

XJCコール

xjc -d out -p forum8941337 schema.xsd 

CreateSubscriberType

以下は

複合型用に生成されたクラスです。

package forum8941337; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlType; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "CreateSubscriberType") 
public class CreateSubscriberType { 

} 

を生成ObjectFactoryクラスが@XmlElementDeclこれらは、あなたのXMLに2つのグローバル要素に対応して、注釈付き2つのcreateのメソッドが含まれていたObjectFactory

スキーマジョダへのdateTime:外部カスタムバインディングはXSから追加されたときに

package forum8941337; 

import javax.xml.bind.JAXBElement; 
import javax.xml.bind.annotation.XmlElementDecl; 
import javax.xml.bind.annotation.XmlRegistry; 
import javax.xml.namespace.QName; 

@XmlRegistry 
public class ObjectFactory { 

    private final static QName _Subscriber_QNAME = new QName("http://www.example.org", "subscriber"); 
    private final static QName _SystemSubscriber_QNAME = new QName("http://www.example.org", "systemSubscriber"); 

    public ObjectFactory() { 
    } 

    public CreateSubscriberType createCreateSubscriberType() { 
     return new CreateSubscriberType(); 
    } 

    @XmlElementDecl(namespace = "http://www.example.org", name = "subscriber") 
    public JAXBElement<CreateSubscriberType> createSubscriber(CreateSubscriberType value) { 
     return new JAXBElement<CreateSubscriberType>(_Subscriber_QNAME, CreateSubscriberType.class, null, value); 
    } 

    @XmlElementDecl(namespace = "http://www.example.org", name = "systemSubscriber") 
    public JAXBElement<CreateSubscriberType> createSystemSubscriber(CreateSubscriberType value) { 
     return new JAXBElement<CreateSubscriberType>(_SystemSubscriber_QNAME, CreateSubscriberType.class, null, value); 
    } 

} 

デモ

package forum8941337; 

import javax.xml.bind.*; 
public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance("forum8941337"); 
     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

     CreateSubscriberType subscriberType = new CreateSubscriberType(); 
     ObjectFactory objectFactory = new ObjectFactory(); 

      // System Subscriber 
     JAXBElement<CreateSubscriberType> systemSubscriber = objectFactory.createSystemSubscriber(subscriberType); 
     marshaller.marshal(systemSubscriber, System.out); 

      // Subscriber 
     JAXBElement<CreateSubscriberType> subscriber = objectFactory.createSubscriber(subscriberType); 
     marshaller.marshal(subscriber, System.out); 
    } 

} 

出力

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<systemSubscriber xmlns="http://www.example.org"/> 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<subscriber xmlns="http://www.example.org"/> 
+0

私はあなたの答えを理解しているので、ObjectFactoryから 'subscriber'または 'systemSubscriber'のインスタンスを取得し、JAXBは別のクラスを生成しません。それが正しいか?しかし、私のxsdには、型が異なる別の要素が含まれています。各要素に対して、mavenプラグインは別のクラスを生成します。異なる名前を持つが、同じタイプの要素に対してのみ、プラグインはクラスを生成しません。 – Mirian

+0

@Mirian - JAXBは、匿名複合型のクラスも生成します。その匿名型がグローバル要素にラップされている場合、JAXB実装はそのクラスに '@ XmlRootElement'アノテーションを追加します。 –

+1

JSR-222を読んで週末を過ごしましたが、maven jaxb plguinでは問題はありますが、xjcコンパイラでは問題があるようです。 xjcコンパイラの助けを借りてクラスを生成すると、私はあなたと同じ絵を手にするでしょう。しかし、Mavenプラグインを使用すると(内部でxjcコンパイラを使用)、Subscriber.javaとSystemSubscriber.javaをCreateSubscriberType.javaに追加します。グローバルな要素のクラスを生成するためのxjcコンパイラ固有のプロパティがあるか教えてください。 – Mirian

0

より深い調査の後、我々はグローバルな要素が生成されることが判明しました。日付時刻。私はxjcコンパイラでそれをテストしました: xjc -extension -b custom-binding.xjb outSchema.xsd。 JAXB 2.1.10では正しく動作していませんか?

関連する問題