2012-07-18 48 views
1

考慮構建使用JAXB這個XML消息:使用JAXB註釋應用Composite模式的最佳方法是什麼?

<msg> 
    <database id="111"> 
    <table name="t1" pkCol="id"> 
     <row op="update"> 
      <col name="id" value="12345"/> 
      <col name="age" value="30"/> 
      <col name="name" value="John"/> 
     </row> 
     <row ...> 
     </row> 
    </table> 
    <table ...> 
     : 
    </table> 
    </database> 
    <database ...> 
    <table ...> 
    </table> 
    </database> 
</msg> 

我們現有的傳統實現具有每個標籤,即味精,數據庫,表,行和列的一個類。沒有共同的基礎類。每個類都有一個List/ArrayList,一個無參數構造函數,以及用於@XmlAttribute和List添加的其他setter。

試圖應用複合模式(其他模式建議?),因爲所有這些類都是非常多的節點(具有子節點)並且還具有它們自己的相應屬性。提出一個共同的基類

class Node<X> extends ArrayList<X> 

但不知道HOWTO從子類中應用的基類的@XmlElement(NAME =「depends_on_subclass」)。除非在子類中,並且在假getSelf()方法上添加@XmlElement,例如在表中,

class Table extends Node<Row> 
{ 

    @XmlElement(name="row") 
    public Table getSelf() 
    { 
     return this; 
    } 
: 
: 

任何其他或更好的建議?提前致謝。

回答

0

我假設你使用XJC爲此,在這種情況下,你可以使用JAXB插件

我用這個插件http://confluence.highsource.org/display/J2B/Inheritance+plugin

再加入這樣的事情你的XML。這就是它。他們將擴展給定的基類。

<xsd:element name="attributesRequest"> 
    <xsd:annotation> 
     <xsd:appinfo> 
      <inheritance:extends>com.work.autofill.common.Filter</inheritance:extends> 
     </xsd:appinfo> 
    </xsd:annotation> 
+0

thx爲您的答案。在我的情況下,沒有給出模式。我們可以返回並生成一個,但是在代碼更改方面是否還有其他方法?或者我錯過了什麼? – user1500049 2012-07-18 01:29:48

相關問題