jaxb 解析和组装嵌套的xml

Liuyfly 2015-03-11 02:52:54
需要组装这样的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.]

...全文
261 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
商科程序员 2015-03-12
  • 打赏
  • 举报
回复
class A
{
     B b;
}

class B
{
      C c;
      D d;
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧