我的用例非常有限,并且我有很多约束。
首先,我只有下面定义的示例结构的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的简单表示(不,我不处理个人、公司数据模型:((。请注意,有许多项目类型。字段类型实际上是我想要创建的POJO。项目之间也存在关系。请注意,这些关系通常是1对多。
即
- 一个人可以扮演很多角色
- 一个人可以为许多董事会工作
- 一个人可以为许多组织工作
还有1对1的关系,即:
- 一个角色只能属于一个组织
- 还有其他一些
我的目标:
- 我想知道提取所有关系的最干净的方法
- 使用正确的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>
有趣的需求组合。我把它用于SimpleXml。这取决于你是否觉得足够干净。首先是一些POJO:
public class Items {
@XmlName("item")
@XmlAbstractClass(tag="type", types={
@TypeMap(name="board", type=Board.class),
@TypeMap(name="role", type=Role.class),
@TypeMap(name="person", type=Person.class)
})
public List<Item> items;
}
abstract class Item {
@XmlName("content_id")
public Integer contentId;
}
public class Board extends Item {
String abn;
}
public class Role extends Item {
@XmlNoExport
@XmlName("role_belongs_to")
Integer boardId;
@XmlNoImport
@XmlWrapperTag("role_belongs_to")
Board board;
}
public class Person extends Item {
@XmlNoExport
@XmlName("has_role")
public Integer hasRole;
@XmlNoImport
public Role role;
}
@XmlName("items")
public class PersonList {
@XmlName("Person")
public List<Person> persons;
}
接下来,我们将xml序列化为项目列表:
final SimpleXml simple = new SimpleXml();
final Items items = simple.fromXml(xml, Items.class);
然后我们需要在内存中进行一些移动:
final Map<Integer, Board> boards = new HashMap<>();
items.items.forEach(item -> { if (item instanceof Board) boards.put(item.contentId, (Board)item); });
final Map<Integer, Role> roles = new HashMap<>();
items.items.forEach(item -> { if (item instanceof Role) roles.put(item.contentId, (Role)item); });
final List<Person> persons = new ArrayList<>();
items.items.forEach(item -> { if (item instanceof Person) persons.add((Person)item); });
roles.values().forEach(role -> role.board = boards.get(role.boardId));
persons.forEach(person -> person.role = roles.get(person.hasRole));
最后序列化结果并打印:
final PersonList pl = new PersonList();
pl.persons = persons;
System.out.println(simple.toXml(pl));
应该打印出你想要的东西。我不太明白你是如何将角色分配给Persons的,所以我制作了一个has_role
字段并使用了它。你当然可以做任何事。
SimpleXml位于maven中心:
<dependency>
<groupId>com.github.codemonstur</groupId>
<artifactId>simplexml</artifactId>
<version>1.5.5</version>
</dependency>