![](/img/trans.png)
[英]JAXB with XmlAdapter for List to Map. Does not marshall as expected but outputs object details
[英]JAXB @XmlAdapter: Map -> List adapter? (marshall only)
我有一個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是否支持此轉換? 或者我忽略了一些文檔部分,它解釋了為什么不是?
PS:我想得到這樣的XML:
<properties>
<property name="protocol"/>
<property name="marshaller"/>
<property name="unmarshaller"/>
<property name="timeout"/>
...
</properties>
我明白了,只需要使用中級課程。 還在XMLCompositeObjectMappingNodeValue.marshalSingleValue(XMLCompositeObjectMappingNodeValue.java:161)的Handle NPE中進行了描述
而是適應的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.