JAXB: Jak powstrzymać otaczający XmlElement podczas używania XmlJavaTypeAdapter?

używam@XmlJavaTypeAdapter przekształcićMap<String, MapItem> przedmioty doList<MapItem> podczas zestawiania (i vice versa, gdy unmarshalling).

Lista zawsze ma otoczenie@XmlElement i chcę się go pozbyć, ponieważ zaśmieca wynikowy XML.

Jak można to zrobić?

Innymi słowy, jak mogę pozbyć się elementumap w następującym XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<top>
    <map>
        <item val="some-val" key="some-key"/>
    </map>
</top>

KlasaMapItemto prosta klasa z kluczem i wartością:

public static class MapItem {
    @XmlAttribute(name = "key")
    String key;

    @XmlAttribute(name = "val")
    String val;
}

DeklaracjaMap<String, MapItem> zawiera niejawnie lub jawnie@XmlElement adnotacja:

@XmlJavaTypeAdapter(MyMapItemAdapter.class)
@XmlElement(name = "map")
Map<String, MapItem> map = new TreeMap<String, MapItem>();

Klasy dla@XmlJavaTypeAdapter:

@XmlType(name = "map-type", propOrder = { "list" })
static class MyMapItemType {
    @XmlElement(name = "item")
    List<MapItem> list = new ArrayList<MapItem>();
}

@XmlTransient
static final class MyMapItemAdapter extends
        XmlAdapter<MyMapItemType, Map<String, MapItem>> {

    MyMapItemAdapter() {
    }

    @Override
    public MyMapItemType marshal(Map<String, MapItem> arg0)
            throws Exception {
        MyMapItemType myMapType = new MyMapItemType();
        for (Entry<String, MapItem> entry : arg0.entrySet()) {
            myMapType.list.add(entry.getValue());
        }
        return myMapType;
    }

    @Override
    public Map<String, MapItem> unmarshal(MyMapItemType arg0)
            throws Exception {
        TreeMap<String, MapItem> treeMap = new TreeMap<String, MapItem>();
        for (MapItem myEntryType : arg0.list) {
            treeMap.put(myEntryType.key, myEntryType);
        }
        return treeMap;
    }
}

Deklaracja najwyższej klasy:

@XmlRootElement(name = "top")
@XmlType(name = "top")
@XmlAccessorType(XmlAccessType.FIELD)
public class JaxbMapTest {

Moja pełna klasa testowa:

package xml;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name = "top")
@XmlType(name = "top")
@XmlAccessorType(XmlAccessType.FIELD)
public class JaxbMapTest {

    public static class MapItem {
        @XmlAttribute(name = "key")
        String key;

        @XmlAttribute(name = "val")
        String val;
    }

    @XmlTransient
    static final class MyMapItemAdapter extends
            XmlAdapter<MyMapItemType, Map<String, MapItem>> {

        MyMapItemAdapter() {
        }

        @Override
        public MyMapItemType marshal(Map<String, MapItem> arg0)
                throws Exception {
            MyMapItemType myMapType = new MyMapItemType();
            for (Entry<String, MapItem> entry : arg0.entrySet()) {
                myMapType.list.add(entry.getValue());
            }
            return myMapType;
        }

        @Override
        public Map<String, MapItem> unmarshal(MyMapItemType arg0)
                throws Exception {
            TreeMap<String, MapItem> treeMap = new TreeMap<String, MapItem>();
            for (MapItem myEntryType : arg0.list) {
                treeMap.put(myEntryType.key, myEntryType);
            }
            return treeMap;
        }
    }

    @XmlType(name = "map-type", propOrder = { "list" })
    static class MyMapItemType {
        @XmlElement(name = "item")
        List<MapItem> list = new ArrayList<MapItem>();
    }

    public static void main(String[] args) {

        try {

            // Setup object
            JaxbMapTest jaxbMapTest = new JaxbMapTest();
            MapItem mapItem = new MapItem();
            mapItem.key = "some-key";
            mapItem.val = "some-val";
            jaxbMapTest.add(mapItem);

            // Marshal
            JAXBContext jaxbContext = JAXBContext
                    .newInstance(JaxbMapTest.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.TRUE);
            marshaller.marshal(jaxbMapTest, new File("JaxbMapTest.out"));

            // Exit
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    @XmlJavaTypeAdapter(MyMapItemAdapter.class)
    @XmlElement(name = "map")
    Map<String, MapItem> map = new TreeMap<String, MapItem>();

    void add(MapItem mapItem) {
        map.put(mapItem.key, mapItem);
    }
}

questionAnswers(1)

yourAnswerToTheQuestion