JAXB封送由XmlAdapter创建的ArrayList

时间:2012-10-31 17:22:30

标签: java arraylist jaxb marshalling xmladapter

我想使用HashMap调整XmlAdapter字段的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>

1 个答案:

答案 0 :(得分:2)

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

}

了解更多信息


<强>更新

  

谢谢,这很有效。然而,我得到了额外的水平   生成的XML中的注释。有没有办法避免这种情况?

如果您使用EclipseLink MOXy作为JAXB (JSR-222)提供商,则可以针对此用例使用@XmlPath扩展名。我将在下面举例说明。

<强>富

hashmap之外的@XmlJavaTypeAdapter属性中,我添加了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>

了解更多信息