需要组装这样的xml:
<A>
<B>
<C>
<C1>test1</C1>
<C2>test2</C2>
<C3>test3</C3>
<C4>test4</C4>
</C>
</B>
</A>
还有这样的:
<A>
<B>
<D>
<D1>test1</D1>
<D2>test2</D2>
<D3>test3</D3>
<D4>test4</D4>
</D>
</B>
</A>
定义A类、B类、C类,A引用B,B引用C,这样肯定能实现。
但是当需要组装第二种xml的时候,还得定义A类(2)、B类(2)、D类,A(2)引用B(2),B(2)引用D。
可以定义一个Abc类,里面直接包含A标签、B标签和C标签;然后定义一个Abd类,里面包含A标签、B标签和D标签么?
或者定义A类、B类、CD类、C类、D类,C类和D类继承CD类,然后在B类里定义的是CD类型,用继承的方式么?
在线等。。。
补充:
我用第二种方式实现,结果报错:
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: Instance of "com.test.C" is substituting "com.test.CD", but "com.test.C" is bound to an anonymous type.]