gpt4 book ai didi

java - SimpleXML 反序列化 Map

转载 作者:行者123 更新时间:2023-11-30 11:09:00 24 4
gpt4 key购买 nike

我有 XML:

<?xml version="1.0" encoding="UTF-8"?>
<addresses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='schema_task1_1.xsd'>

<address>
<name>Bill</name>
<city>NewYork</city>
<street>First</street>
<phone type='mobile'>+19139130000</phone>
<documents>
<document type='driving_permit'>9045798749</document>
<document type='passport'>3451231231</document>
</documents>
</address>

<address>
...
</address>

</addresses>

Java 类:

@Root(name = "addresses")
public class Addresses {

@ElementList(inline = true, entry = "address", required = false, type = Address.class)
private List<Address> addresses = new ArrayList<Address>();

public class Address {

@Element(required = false)
private String name;
@Element(required = false)
private String city;
@Element(required = false)
private String street;
@ElementMap(entry = "phone", key = "type", attribute = true, inline = true)
private Map<String, String> phones;
@ElementMap(entry = "documents", key = "type", attribute = true, inline = true)
private Map<String, String> documents = new HashMap<String, String>();

所有字段反序列化成功,文档除外。尝试反序列化时它始终为 null。我想我在 @ElementMap 注释中有错误,但我不知道到底是什么。我做错了什么?

附言。我无法更改对象结构,因为它已被其他解析器使用。

最佳答案

您必须创建一个中间类来处理“文档”。此类将包含您的“文档” map :

class Address {
...
@Element(required = false)
private Documents documents;
}
class Documents {
@ElementMap(entry = "document", key = "type", attribute = true, inline = true)
private Map<String, String> documents = new HashMap<String, String>();
}

关于java - SimpleXML 反序列化 Map<String, String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28301476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com