JAXB @XmlAdapter:Map - > List adapter?(仅限马歇尔)

Ond*_*žka 8 java jaxb marshalling moxy xmladapter

我有一个Map<String, String>.
每个人的第一个想法是将其转换为List<Pair<String,String>>(Pair作为自定义类).

我试过@XmlAdapter这样的:

public class MapPropertiesAdapter extends XmlAdapter<List<Property>, Map<String,String>> { ... }
Run Code Online (Sandbox Code Playgroud)

但Eclipse MOXy,我使用的JAXB impl,最终得到了ClassCastException- "无法将HashMap转换为Collection".

JAXB是否支持此转换?或者我忽略了一些文档部分,它解释了为什么不是?

PS:我想得到这样的XML:

<properties>
    <property name="protocol"/>
    <property name="marshaller"/>
    <property name="unmarshaller"/>
    <property name="timeout"/>
    ...
</properties>
Run Code Online (Sandbox Code Playgroud)

我明白了,只需要使用中级课程.还在XMLCompositeObjectMappingNodeValue.marshalSingleValue(XMLCompositeObjectMappingNodeValue.java:161)中的Handle NPE中进行了描述

bdo*_*han 9

您应该将其调整为具有属性的对象,而不是将其调整Map为a .ListList

XmlAdapter(MapPropertiesAdapter)

import java.util.*;
import java.util.Map.Entry;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MapPropertiesAdapter extends XmlAdapter<MapPropertiesAdapter.AdaptedProperties, Map<String, String>>{

    public static class AdaptedProperties {
        public List<Property> property = new ArrayList<Property>();
    }

    public static class Property {
        @XmlAttribute
        public String name;

        @XmlValue
        public String value;
    }

    @Override
    public Map<String, String> unmarshal(AdaptedProperties adaptedProperties) throws Exception {
        if(null == adaptedProperties) {
            return null;
        }
        Map<String, String> map = new HashMap<String, String>(adaptedProperties.property.size());
        for(Property property : adaptedProperties.property) {
            map.put(property.name, property.value);
        }
        return map;
    }

    @Override
    public AdaptedProperties marshal(Map<String, String> map) throws Exception {
        if(null == map) {
            return null;
        }
        AdaptedProperties adaptedProperties = new AdaptedProperties();
        for(Entry<String,String> entry : map.entrySet()) {
            Property property = new Property();
            property.name = entry.getKey();
            property.value = entry.getValue();
            adaptedProperties.property.add(property);
        }
        return adaptedProperties;
    }

}
Run Code Online (Sandbox Code Playgroud)

域模型(根)

下面是具有Map属性的模型对象.该@XmlJavaTypeAdapter注释用于指定XmlAdapter.

import java.util.Map;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlJavaTypeAdapter(MapPropertiesAdapter.class)
    private Map<String, String> properties;

}
Run Code Online (Sandbox Code Playgroud)

演示

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum17024050/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}
Run Code Online (Sandbox Code Playgroud)

input.xml中/输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <properties>
        <property name="A">a</property>
        <property name="B">b</property>
    </properties>
</root>
Run Code Online (Sandbox Code Playgroud)