【发布时间】:2018-07-23 06:05:53
【问题描述】:
我的用例非常有限,而且我有很多限制。
首先,我只有一个如下定义的示例结构的 XML。我没有架构定义。我的要求和实施在方法上受到很大限制。
<?xml version="1.0" encoding="UTF-8" ?>
<items>
<item>
<content_id>56789012</content_id>
<unique_record_id>B-123456</unique_record_id>
<title>ABC</title>
<type>board</type>
<dfield>098765</dfield>
<abn>11 222 333 444</abn>
<cfield>Yes</cfield>
<bfield>Goodness me</bfield>
<afield>ABCD</afield>
</item>
<item>
<content_id>1234</content_id>
<unique_record_id>D-789</unique_record_id>
<title>Member</title>
<type>role</type>
<contact>90000</contact>
<role_belongs_to>56789012</role_belongs_to>
<updated>23/07/2018 - 3:30pm</updated>
<importance>90</importance>
</item>
<item>
<content_id>90000</content_id>
<unique_record_id>D-654321</unique_record_id>
<title>Someone Else</title>
<type>person</type>
<salutation>Ms.</salutation>
<first_name>Someone</first_name>
<last_name>Else</last_name>
</item>
.
.
.
.
.
</items>
上面的模型是我将给出的 xml 的简单表示(我不处理人,公司数据模型:))。请注意,有许多项目类型。 type 字段实际上是我想要创建的 POJO。物品之间也有关系。请注意,关系通常是一对多。
即
- 一个人可以有很多角色
- 一个人可以为多个董事会工作
- 一个人可以为许多组织工作
也有一对一的关系,即:
- 一个角色只能属于一个组织
- 还有其他一些......
我的目标:
- 我想知道提取所有关系的最干净的方法
- 使用正确的所有项目创建关联的 pojo 列表 pojo类型
- 将 xml 非规范化为下面定义的新 xml 结构示例。
我知道我可以编写相当多的代码来剥离 xml,并在 xml 被解组和迭代后使用反射来创建 POJO。
我正在寻找最简洁的方法,我对 JAXB、SAX 或任何其他可以简化手头任务的库持开放态度。
非规范化输出示例:
<?xml version="1.0" encoding="UTF-8" ?>
<items>
<Person>
<content_id>90000</content_id>
<unique_record_id>D-654321</unique_record_id>
<title>Someone Else</title>
<salutation>Ms.</salutation>
<first_name>Someone</first_name>
<last_name>Else</last_name>
<role>
<content_id>1234</content_id>
<unique_record_id>D-789</unique_record_id>
<title>Member</title>
<role_belongs_to>
<board>
<content_id>56789012</content_id>
<unique_record_id>B-123456</unique_record_id>
<title>ABC</title>
<dfield>098765</dfield>
<abn>11 222 333 444</abn>
<cfield>Yes</cfield>
<bfield>Goodness me</bfield>
<afield>ABCD</afield>
</board>
</role_belongs_to>
<updated>23/07/2018 - 3:30pm</updated>
<importance>90</importance>
</role>
</Person>
.
.
</items>
【问题讨论】:
标签: java xml jaxb unmarshalling