[英]JAXB Marshall Map<Integer, ArrayList<String>>
I have an object i'd like to marshall. 我有一个我想要编组的对象。
@XmlRootElement
public class BoxItem {
@XmlElement
Map<Integer, ArrayList<String>> intgerStringArrMap;
BoxItem() {
intgerStringArrMap = new HashMap<Integer, ArrayList<String>>();
for (int i = 0; i < 3; i++) {
ArrayList<String> stringArrayList = new ArrayList<String>();
for (int j = 0; j < 10; j++) {
stringArrayList.add(new BigInteger(130, new SecureRandom()).toString(32));
}
intgerStringArrMap.put(i, stringArrayList);
}
}
}
Now let's assume we have a boxItem = new BoxItem() 现在让我们假设我们有一个boxItem = new BoxItem()
If i call jaxbMarshaller.marshal(boxItem, System.out);, the values are empty for each entry. 如果我调用jaxbMarshaller.marshal(boxItem,System.out);,则每个条目的值都为空。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<boxItem>
<intgerStringArrMap>
<entry>
<key>0</key>
<value/>
</entry>
<entry>
<key>1</key>
<value/>
</entry>
<entry>
<key>2</key>
<value/>
</entry>
</intgerStringArrMap>
</boxItem>
How to marshall the elements inside the ArrayList in a Map value? 如何在Map值中对ArrayList中的元素进行编组?
You should use @XmlElementWrapper Annotation.They are used to produce a wrapper XML element around Collections
您应该使用@XmlElementWrapper Annotation。它们用于围绕
Collections
生成包装器XML元素
Define a wrapper for your ArrayList
like below, 为
ArrayList
定义一个包装器,如下所示,
class ListWrapper {
@XmlElementWrapper(name = "wrapperList")
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
}
Define your Map as below in BoxItem
class, 在
BoxItem
类中定义如下地图,
@XmlElementWrapper(name = "integerMap")
Map<Integer, ListWrapper> intgerStringArrMap;
Here is the complete class. 这是完整的课程。
@XmlRootElement
public class BoxItem {
@XmlElementWrapper(name = "integerMap")
Map<Integer, ListWrapper> intgerStringArrMap;
BoxItem() {
intgerStringArrMap = new HashMap<Integer, ListWrapper>();
for (int i = 0; i < 2; i++) {
ArrayList<String> stringArrayList = new ArrayList<String>();
ListWrapper wrapper = new ListWrapper();
wrapper.setList(stringArrayList);
for (int j = 0; j < 2; j++) {
stringArrayList.add("2");
}
intgerStringArrMap.put(i, wrapper);
}
}
public static void main(String[] args) throws JAXBException {
BoxItem box = new BoxItem();
JAXBContext jc = JAXBContext.newInstance(BoxItem.class);
jc.createMarshaller().marshal(box, System.out);
}
}
class ListWrapper {
@XmlElementWrapper(name = "wrapperList")
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
}
Running the above should get the below output, 运行以上应该得到以下输出,
<boxItem>
<integerMap>
<entry>
<key>0</key>
<value>
<wrapperList>
<list>2</list>
<list>2</list>
</wrapperList>
</value>
</entry>
<entry>
<key>1</key>
<value>
<wrapperList>
<list>2</list>
<list>2</list>
</wrapperList>
</value>
</entry>
</integerMap>
</boxItem>
Jayamohan's answer works, and perhaps is the preferred solution, but if you're ever in a case where you prefer not to modify the RootElement class (BoxItem), you can write your own XmlAdapter
so that JAXB knows how to handle cases like Map<Integer, ArrayList<String>>
. Jayamohan的答案是有效的,也许是首选的解决方案,但是如果您曾经不想修改RootElement类(BoxItem),您可以编写自己的
XmlAdapter
以便JAXB知道如何处理Map<Integer, ArrayList<String>>
。
Please refer to How to marshall Map<String, List<Objects>> using JAXB for writing your own XmlAdapter 请参阅如何使用JAXB编写Map <String,List <Objects >>来编写自己的XmlAdapter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.