2013-06-10 3 views
7

J'ai un Map<String, String>.
La première idée que tout le monde a est de le convertir en List<Pair<String,String>> (Pair étant une classe personnalisée).JAXB @XmlAdapter: Carte -> Carte de liste? (marshall seulement)

J'ai essayé @XmlAdapter comme ceci:

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

Mais Eclipse Moxy, les impl JAXB que j'utilise, a fini avec un ClassCastException - "ne peut convertir HashMap Collection".

Cette conversion est-elle prise en charge par JAXB? Ou ai-je oublié une partie de la documentation qui explique pourquoi ce n'est pas?

PS: Je voulais obtenir XML comme ceci:

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

Je l'ai, ne devait utiliser une classe intermédiaire. On décrit également à Handle NPE in XMLCompositeObjectMappingNodeValue.marshalSingleValue(XMLCompositeObjectMappingNodeValue.java:161)

Répondre

8

au lieu d'adapter le Map à un List, vous devez l'adapter à un objet qui a une propriété 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; 
    } 

} 

Model Domain (racine)

Ci-dessous un modèle d'objet avec une propriété Map. L'annotation @XmlJavaTypeAdapter est utilisée pour spécifier le 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; 

} 

Démo

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

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

Avez-vous déjà envisagé de créer une bibliothèque java avec beaucoup d'adaptateurs utiles? –

+0

Ce qui change à faire si à la place de Map , nous avons Map > ??? – Anand

Questions connexes