简体   繁体   中英

How to Marshall HashMap<String, ArrayList<String>> using JAXB

I am trying to marshall HashMap> but ArrayList is comming as empty in resulting xml.

I tried the following code.

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 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. Please help me to fix this issue.

I am expecting XML with all the Names as below,

<?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 . Although the value is a list of items, for the map it is still one value.

JAXB cannot handle complex/nested data structures such as Map<String, List<String>> . You have to create a wrapper ex. WrappedStringList for the values of the map, ie Map<String, WrappedStringList> .

class WrappedStringList {

    private List<String> wrapped;

    // getter & setter
}

It will be used in the object CrossReference such as:

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

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