[英]How to Marshall HashMap<String, ArrayList<String>> using JAXB
I am trying to marshall HashMap> but ArrayList is comming as empty in resulting xml.我正在尝试编组 HashMap>,但 ArrayList 在生成 xml 时为空。
I tried the following code.我尝试了以下代码。
POJO - POJO -
@XmlRootElement(name = "crossreference")
@XmlAccessorType(XmlAccessType.FIELD)
public class CrossReference implements Serializable {
private static final long serialVersionUID = 1L;
HashMap<String, ArrayList<String>> testMap = new HashMap<>();
public HashMap<String, ArrayList<String>> getTestMap() {
return testMap;
}
public void setTestMap(HashMap<String, ArrayList<String>> testMap) {
this.testMap = testMap;
}
@Override
public String toString() {
return "CrossReference [testMap=" + testMap + "]";
}
Code to Marshall,马歇尔的代码,
ArrayList<String> nameList = new ArrayList<>();
nameList.add("Shivling");
nameList.add("Bipin");
nameList.add("Sudhakar");
HashMap<String, ArrayList<String>> testMap = new HashMap<>();
testMap.put("name", nameList);
CrossReference crossReference = new CrossReference();
crossReference.setTestMap(testMap);
JAXBContext context = JAXBContext.newInstance(CrossReference.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML
StringWriter stringWriter = new StringWriter();
marshaller.marshal(crossReference, stringWriter);
String xml = stringWriter.toString();
System.out.println(xml);
XML after doing Marshalling -编组后的 XML -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<crossreference>
<testMap>
<entry>
<key>name</key>
<value/>
</entry>
</testMap>
</crossreference>
I am not sure why the value is empty in above XML.我不确定为什么 XML 上面的值是空的。 Please help me to fix this issue.请帮我解决这个问题。
I am expecting XML with all the Names as below,我期待 XML 的所有名称如下,
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<crossreference>
<testMap>
<entry>
<key>name</key>
<value>Shivling</value>
<value>Bipin</value>
<value>Sudhakar</value>
</entry>
</testMap>
</crossreference>
First of all, you cannot have such a result using this kind of structure since Map
has only one value
.首先,由于Map
只有一个value
,因此使用这种结构是不可能得到这样的结果的。 Although the value
is a list of items, for the map it is still one value.尽管该value
是项目列表,但对于 map,它仍然是一个值。
JAXB cannot handle complex/nested data structures such as Map<String, List<String>>
. JAXB 无法处理复杂/嵌套的数据结构,例如Map<String, List<String>>
。 You have to create a wrapper ex.你必须创建一个包装前。 WrappedStringList
for the values of the map, ie Map<String, WrappedStringList>
. WrappedStringList
的值的 WrappedStringList,即Map<String, WrappedStringList>
。
class WrappedStringList {
private List<String> wrapped;
// getter & setter
}
It will be used in the object CrossReference
such as:它将用于 object CrossReference
参考,例如:
@XmlElementWrapper(name = "testMap")
HashMap<String, WrappedStringList> testMap = new HashMap<>();
The result looks like:结果如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<crossreference>
<testMap>
<entry>
<key>name</key>
<value>
<wrapped>Shivling</wrapped>
<wrapped>Bipin</wrapped>
<wrapped>Sudhakar</wrapped>
</value>
</entry>
</testMap>
</crossreference>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.