2010-07-12 63 views
2

在我的模式中,我有一些類型只是簡單地擴展了一個簡單的XSD類型(int或string)。 JAXB根據這種類型創建一個單獨的java類。我想放棄這個中間類並配置JAXB在可能的地方使用原語(例如用CountryTypejava.lang.StringDocumentTypeintlava.lang.Integer)。例如,對於給定的XSD,最好有DestinationType.setDocumentType(int)List<String> StatesType.getCountry()。我很樂意爲此編寫類型爲an adapter,但看起來只支持來自原始XML類型的轉換。也許有可能使每個屬性類型轉換?請給出任何JAXB綁定定製的例子,這可以提供幫助。JAXB用原始類型替換複雜類型

<?xml version="1.0" encoding="UTF-8"?> 
<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    xmlns:exch="http://www.mycompany.org/exchange" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    targetNamespace="http://www.mycompany.org/exchange" 
    elementFormDefault="qualified" attributeFormDefault="unqualified"> 

    <complexType name="countryType"> 
     <simpleContent> 
      <extension base="string"/> 
     </simpleContent> 
    </complexType> 

    <complexType name="statesType"> 
     <sequence maxOccurs="unbounded"> 
      <element name="country" type="exch:countryType"/> 
     </sequence> 
    </complexType> 

    <complexType name="documentType"> 
     <simpleContent> 
      <extension base="integer"/> 
     </simpleContent> 
    </complexType> 

    <complexType name="destinationType"> 
     <sequence> 
      <element name="states" type="exch:statesType" maxOccurs="1"/> 
      <element name="document-type" type="exch:documentType" minOccurs="1" maxOccurs="1"/> 
     </sequence> 
    </complexType> 
</schema> 

回答

2

另一種可能性,你可以改變你的模式?:

以下模式變化將產生所需的對象模型。

用途:

<simpleType name="documentType"> 
    <restriction base="integer"/> 
</simpleType> 

相反的:

<complexType name="documentType"> 
    <simpleContent> 
     <extension base="integer"/> 
    </simpleContent> 
</complexType> 
+0

可這種轉變沒有打破序列化的XML的兼容性應用? – 2010-07-13 12:51:46

+0

@dma_k - 是的,這兩種類型的XML表示形式都是相同的。 – 2010-07-14 15:17:52

0

好問題。在過去,我通過XSLT預處理步驟來運行模式來解決這個問題,在保留文檔的語義的同時「展開」類型層次結構。

例如,XSLT將刪除documentType類型的定義,並將每個引用替換爲documentTypeinteger。生成的處理過的模式仍然表示相同的實例文檔,但更簡單,並且具有更好的綁定。

這個(而不是一半)解決方案可以應用於許多類似的問題,以處理過於複雜的模式(例如,用選擇結構替換替代組)。