我想使用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>
答案 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>
了解更多信息