java – JAXB编组由XmlAdapter创建的ArrayList

我想使用XmlAdapter调整HashMap字段的
XML表示.我使用ArrayList来做到这一点.但是,当编组ArrayList时根本没有编组.这是为什么?

代码

@XmlRootElement
public class Foo {

    private HashMap<String, String> hashMap;

    public Foo() {
        this.hashMap = new HashMap<String, String>();
    }

    @XmlJavaTypeAdapter(HashMapAdapter.class)
    public HashMap<String, String> getHashmap() {
        return hashMap;
    }

    public void setHashmap(HashMap<String, String> hashMap) {
        this.hashMap = hashMap;
    }

}
public final class HashMapAdapter extends XmlAdapter<ArrayList<HashMapEntry>, HashMap<String, String>> {

    @Override
    public ArrayList<HashMapEntry> marshal(HashMap<String, String> arg0) throws Exception {
        ArrayList<HashMapEntry> result = new ArrayList<HashMapEntry>();
        for(Entry<String, String> entry : arg0.entrySet())
            result.add(new HashMapEntry(entry.getKey(), entry.getValue()));
        return result;
    }

    @Override
    public HashMap<String, String> unmarshal(ArrayList<HashMapEntry> arg0) throws Exception {
        HashMap<String, String> result = new HashMap<String, String>();
        for(HashMapEntry entry : arg0)
            result.put(entry.key, entry.value);
        return result;
    }

}
public class HashMapEntry {

    @XmlElement 
    public String key;

    @XmlValue
    public String value;

    public HashMapEntry() {

    }

    public HashMapEntry(String key, String value) {
        this.key = key;
        this.value = value;
    }
}

结果

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><foo><hashmap/></foo>

最佳答案 在XmlAdapter中,您需要将HashMap转换为具有List属性的对象实例,而不是直接转换为ArrayList.

HashMapAdapter

package forum13163430;

import java.util.*;
import java.util.Map.Entry;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public final class HashMapAdapter extends XmlAdapter<HashMapAdapter.AdaptedHashMap, HashMap<String, String>> {

    @Override
    public AdaptedHashMap marshal(HashMap<String, String> hashMap) throws Exception {
        AdaptedHashMap adaptedHashMap = new AdaptedHashMap();
        for(Entry<String, String> entry : hashMap.entrySet()) {
            adaptedHashMap.item.add(new HashMapEntry(entry.getKey(), entry.getValue()));
        }
        return adaptedHashMap;
    }

    @Override
    public HashMap<String, String> unmarshal(AdaptedHashMap adaptedHashMap) throws Exception {
        HashMap<String, String> result = new HashMap<String, String>();
        for(HashMapEntry entry : adaptedHashMap.item)
            result.put(entry.key, entry.value);
        return result;
    }

    public static class AdaptedHashMap {
        public List<HashMapEntry> item = new ArrayList<HashMapEntry>();
    }

    public static class HashMapEntry {

        @XmlAttribute 
        public String key;

        @XmlValue
        public String value;

        public HashMapEntry() {
        }

        public HashMapEntry(String key, String value) {
            this.key = key;
            this.value = value;
        }
    }

}

欲获得更多信息

> http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html

UPDATE

Thanks, this works. However then I get an additional level of
annotation in the produced XML. Is there any way to avoid that?

如果您使用EclipseLink MOXy作为JAXB (JSR-222)提供程序,则可以在此用例中使用@XmlPath扩展.我将在下面举例说明.

在@XmlJavaTypeAdapter之外的hashmap属性上,我添加了MOXy的@XmlPath注释. XML路径“.”表示应将子项编组到父XML元素中.

package forum13163430;

import java.util.HashMap;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
public class Foo {

    private HashMap<String, String> hashMap;

    public Foo() {
        this.hashMap = new HashMap<String, String>();
    }

    @XmlPath(".")
    @XmlJavaTypeAdapter(HashMapAdapter.class)
    public HashMap<String, String> getHashmap() {
        return hashMap;
    }

    public void setHashmap(HashMap<String, String> hashMap) {
        this.hashMap = hashMap;
    }

}

jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域模型相同的包中包含一个名为jaxb.properties的文件,并带有以下条目(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

演示

由于MOXy是符合JAXB(JSR-222)的实现,因此标准API可用于将对象从/转换为XML.

package forum13163430;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum13163430/input.xml");
        Foo foo = (Foo) unmarshaller.unmarshal(xml);

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

}

input.xml中/输出

下面是运行演示代码的输入和输出.

<?xml version="1.0" encoding="UTF-8"?>
<foo>
   <item key="b">B</item>
   <item key="c">C</item>
   <item key="a">A</item>
</foo>

欲获得更多信息

> http://blog.bdoughan.com/2010/07/xpath-based-mapping.html

点赞