2013-06-10 13 views
7

Ho un Map<String, String>.
La prima idea che tutti hanno è di convertirla in un List<Pair<String,String>> (Pair di essere una classe personalizzata).JAXB @XmlAdapter: Map -> List adapter? (solo marshall)

Ho provato un @XmlAdapter come questo:

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

Ma Eclipse Moxy, i impl JAXB che uso, finito con un ClassCastException - "impossibile convertire HashMap a Collection".

Questa conversione è supportata da JAXB? O ho trascurato parte della documentazione che spiega perché non lo è?

PS: ho voluto ottenere XML in questo modo:

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

ho preso, solo dovuto usare una classe intermedia. Anche descritto in Handle NPE in XMLCompositeObjectMappingNodeValue.marshalSingleValue(XMLCompositeObjectMappingNodeValue.java:161)

risposta

8

Invece di adattare il Map ad un List, si dovrebbe adattare a un oggetto che ha una proprietà 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; 
    } 

} 

Domain Model (Root)

seguito è un oggetto del modello con una proprietà Map. L'annotazione @XmlJavaTypeAdapter viene utilizzata per specificare 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; 

} 

Demo

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/Output

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

Avete mai pensato di creare una libreria Java con un sacco di adattatori utili? –

+0

Quali modifiche devono essere eseguite se anziché Map , abbiamo Map > ??? – Anand

Problemi correlati