简体   繁体   中英

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()

If i call jaxbMarshaller.marshal(boxItem, System.out);, the values are empty for each entry.

<?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?

You should use @XmlElementWrapper Annotation.They are used to produce a wrapper XML element around Collections

Define a wrapper for your ArrayList like below,

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,

@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>> .

Please refer to How to marshall Map<String, List<Objects>> using JAXB for writing your own XmlAdapter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM