提问者:小点点

JAXB马歇尔映射<整数,数组列表<字符串>>


我有个东西想送给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);
      }
  }
}

现在假设我们有一个boxItem=new BoxItem()

如果我调用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>

如何在Map值中编组ArrayList内的元素?


共2个答案

匿名用户

您应该使用@XmlElementWrapper注释。它们用于在Collection周围生成包装XML元素

为您的ArrayList定义一个包装器,如下所示,

class ListWrapper {

    @XmlElementWrapper(name = "wrapperList")
    private List<String> list;

    public void setList(List<String> list) {
        this.list = list;
    }
}

BoxItem类中定义您的Map如下,

@XmlElementWrapper(name = "integerMap")
Map<Integer, ListWrapper> intgerStringArrMap;

这是完整的班级。

@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;
    }
}

运行上面应该得到下面的输出,

<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的答案是可行的,也许是首选的解决方案,但是如果您不喜欢修改RootElement类(BoxItem),您可以编写自己的XmlAdapter以便JAXB知道如何处理Map之类的情况

请参阅如何马歇尔地图