2012-01-20 59 views
3

XSD架構包含具有相同類型的兩個不同元素:JAXB不產生對於相同類型的不同元件類

<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架構包含具有相同類型的兩個不同元素: 然後我嘗試使用maven-jaxb2-plugin從這個xsd生成類,並且沒有結果。這些類沒有生成。如果我修改其中一個元素的類型,它將正常工作,將生成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

生成ObjectFactory類包含與@XmlElementDecl這些註釋2種create方法對應於兩個全局元素在你的XML架構。

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獲得'訂戶'或'systemSubscriber'的實例,JAXB不會生成單獨的類。這是對的嗎?但是我的xsd包含不同類型的另一個元素,並且maven插件爲每個元素生成單獨的類。只有具有不同名稱但具有相同類型的元素不會生成類。 – Mirian

+0

@Mirian - JAXB也會爲匿名複雜類型生成類。如果該匿名類型包裝在全局元素中,那麼JAXB實現將爲該類添加一個「@ XmlRootElement」註釋。 –

+1

我花了一個週末閱讀JSR-222,看起來像我有一個Maven jaxb plguin的問題,但不是xjc編譯器。如果我在xjc編譯器的幫助下生成類,我會得到和你一樣的圖片。但是如果我使用maven插件(在它內部使用xjc編譯器),我會在CreateSubscriberType.java之外獲得Subscriber.java和SystemSubscriber.java。你能告訴我有沒有xjc編譯器爲全局元素生成類的特定屬性? – Mirian

0

後順藤摸瓜,我們發現,當外部的自定義綁定從XS添加全局元素生成:日期時間到喬達。約會時間。我在xjc編譯器上測試了它: xjc -extension -b custom-binding.xjb outSchema.xsd。 JAXB 2.1.10的行爲是否正確?