我的用例非常有限,并且有很多限制。
首先,我只有一个示例结构的 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 个组织
- 还有其他一些......
我的目标:
- 我想知道提取所有关系的最简洁方法
- 使用正确的内容创建所有项目的关联 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>
最佳答案
有趣的需求组合。我让它与 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));
应该打印出你想要的内容。我不太明白您如何将角色分配给人员,因此我创建了一个 has_role
字段并使用它。当然你可以做任何事。
SimpleXml 位于 Maven 中心:
<dependency>
<groupId>com.github.codemonstur</groupId>
<artifactId>simplexml</artifactId>
<version>1.5.5</version>
</dependency>
关于java - 如何在java中从xml实体字符串值创建不同的pojo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51472440/