2012-05-24 37 views
6

我試圖從幾個特定的​​XSD中用xjc生成Java類。這些模式有一些共同的定義,因此它們導入了許多常見的XSD。特別是,它們可以包含從零到所有常見的XSD。使用xjc和綁定定義常見XSD的包名稱

我想將特定XSD中的所有類生成爲特定的包,但將生成的普通模式的類保留在公共包中,這樣它們就不會針對源樹中的每個特定模式重複。

我已經瞭解到,自定義綁定可以用來在每個模式的基礎上指定封裝,例如:

<jxb:bindings schemaLocation="common1.xsd" node="/xsd:schema"> 
    <jxb:schemaBindings> 
     <jxb:package name="mypackage.commonclasses"/> 
    </jxb:schemaBindings> 
</jxb:bindings> 

我有以下結構:

schemas 
| - common 
| | - common1.xsd --> XSD with common types #1 
| | - ... 
| | - commonN.xsd --> XSD with common types #N 
| | - commonBindings.xjb --> Defines package "mypackage.commons" for common*.xsd 
| - specific1 
| | - specific1.xsd --> Includes ../common/common{1-N}.xsd 
| | - specific1.xjb --> Defines package "mypackage.specific1" for specific1.xsd 
| - specificN 
| | - specificN.xsd --> Includes only ../common/common1.xsd 
| | - specificN.xjb --> Defines package "mypackage.specificN" for specificN.xsd 

它可以正常工作:

xjc -b schemas/specific1 
    -b schemas/common 
    schemas/specific1/specific1.xsd 

它生成中的specific1.xsd的類和mypackage.commons中的常見類。但是,當我嘗試生成的類specificNxjc引發以下錯誤:

[ERROR] "file:/drive/dir/schemas/common/common1.xsd" is not a part of 
this compilation. Is this a mistake for "/drive/dir/schemas/common/commonBindings.xjb"? 
    line 2 of file:/drive/dir/schemas/common/commonBindings.xjb 

我得到這個錯誤重複在任何特定的XSD不導入每個公共XSD。

有沒有什麼辦法可以讓xjc忽略在commonBindings.xjb中沒有在XSD中使用的綁定我正在爲類生成類?

或者,我是否使用這種方法瞄準了錯誤的方向,應該是,例如,在特定的xsd中使用註釋?如果可能的話,我想避免修改模式。

回答

3

我認爲你需要的是使用JAXB插曲。請參閱this SO post,看看@BlaiseDoughan提供的答案。

對於您的情況,首先運行xjc,使用您的自定義綁定文件進行軟件包名稱自定義,針對所有常見的XSD,生成劇集文件和常用的Java類。

然後通過引用第一次運行中的情節文件,再次使用xjc和其他想要的XSD。