如何编组 HashMap<String, ArrayList<String>> 使用 JAXB
How to Marshall HashMap<String, ArrayList<String>> using JAXB
我正在尝试编组 HashMap> 但 ArrayList 在结果 xml.
中显示为空
我尝试了以下代码。
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 + "]";
}
给 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 进行编组后 -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<crossreference>
<testMap>
<entry>
<key>name</key>
<value/>
</entry>
</testMap>
</crossreference>
我不知道为什么上面XML中的值是空的。请帮我解决这个问题。
我期待 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>
首先,由于Map
只有一个value
,所以使用这种结构不可能得到这样的结果。虽然 value
是一个项目列表,但对于地图来说它仍然是一个值。
JAXB 无法处理 complex/nested 数据结构,例如 Map<String, List<String>>
。您必须创建一个包装器 ex。 WrappedStringList
表示地图的值,即 Map<String, WrappedStringList>
.
class WrappedStringList {
private List<String> wrapped;
// getter & setter
}
它将在对象CrossReference
中使用,例如:
@XmlElementWrapper(name = "testMap")
HashMap<String, WrappedStringList> testMap = new HashMap<>();
结果如下:
<?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>
我正在尝试编组 HashMap> 但 ArrayList 在结果 xml.
中显示为空我尝试了以下代码。
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 + "]";
}
给 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 进行编组后 -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<crossreference>
<testMap>
<entry>
<key>name</key>
<value/>
</entry>
</testMap>
</crossreference>
我不知道为什么上面XML中的值是空的。请帮我解决这个问题。
我期待 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>
首先,由于Map
只有一个value
,所以使用这种结构不可能得到这样的结果。虽然 value
是一个项目列表,但对于地图来说它仍然是一个值。
JAXB 无法处理 complex/nested 数据结构,例如 Map<String, List<String>>
。您必须创建一个包装器 ex。 WrappedStringList
表示地图的值,即 Map<String, WrappedStringList>
.
class WrappedStringList {
private List<String> wrapped;
// getter & setter
}
它将在对象CrossReference
中使用,例如:
@XmlElementWrapper(name = "testMap")
HashMap<String, WrappedStringList> testMap = new HashMap<>();
结果如下:
<?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>