java - JAXB @XmlAdapter : Map -> List adapter?(仅限编码)

标签 java jaxb marshalling moxy xmladapter

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

我试过 @XmlAdapter像这样:

public class MapPropertiesAdapter extends XmlAdapter<List<Property>, Map<String,String>> { ... }

但是 Eclipse MOXy,我使用的 JAXB impl,以 ClassCastException 结束。 - “无法将 HashMap 转换为 Collection”。

JAXB 支持这种转换吗?还是我忽略了一些解释为什么不是这样的文档部分?

附言: 我想得到这样的 XML:

<properties>
    <property name="protocol"/>
    <property name="marshaller"/>
    <property name="unmarshaller"/>
    <property name="timeout"/>
    ...
</properties>

我明白了,只需要使用中级类(class)。也描述于 Handle NPE in XMLCompositeObjectMappingNodeValue.marshalSingleValue( XMLCompositeObjectMappingNodeValue.java:161)

最佳答案

与其将 Map 适配为 List,不如将其适配为具有 List 属性的对象。

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

}

域模型(根)

下面是一个带有 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;

}

演示

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

}

input.xml/输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <properties>
        <property name="A">a</property>
        <property name="B">b</property>
    </properties>
</root>

关于java - JAXB @XmlAdapter : Map -> List adapter?(仅限编码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17024050/

相关文章:

java - Spring @Transactional(readOnly=true) 恢复属性?

java - 如何在 Spring 中向多个收件人发送电子邮件

tomcat - 无法重新部署应用程序(使用 JAXB)

java - Marshall 具有 OnetoMany 关系的 hibernate pojo 类

c# - 将 C# 字典编码到 C++(非托管)

java - ViewPager PagerAdapter 不更新 View

java - 如何使用条件生成器使用多个过滤器进行搜索

java - JaxB 如何将列表的内容编码为单独的标签

java - JAXB 不调用 Setter 方法

.net - C# 中的编码(marshal) "char *"