2012-07-18 52 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

相关问题