J'essaie d'utiliser l'implémentation JAXB pour convertir mes objets de classe en XML et vice versa. J'ai implémenté ceci en observant un tutoriel.Implémentation de JAXB sans classes POJO et avec linkedHashmaps
Mon XML ressemble à ceci
<Alphabet>
<a>Apple</a>
<b>Ball</b>
<c>Cat</c>
<d>Dog</d>
<e>Elephant</e>
<f>Fox</f>
</Alphabet>
j'ai écrit les classes suivantes
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
public class Alph {
@XmlElement
private LinkedHashMap<String, String> cLinkedHashMap = new
LinkedHashMap<String, String>();
protected void put(String theKey, String theValue) {
cLinkedHashMap.put(theKey, theValue);
}
protected String get(String theKey) {
return (String) cLinkedHashMap.get(theKey);
}
protected Set<Entry<String,String>> getCEntrySet() {
return cLinkedHashMap.entrySet();
}
protected LinkedHashMap<String, String> getCLinkedHashMap() {
return cLinkedHashMap;
}
public String toCXML() throws XMLHandlingException {
return null;
}
}
@XmlRootElement
public class Alphabet extends Alph {
public static Alphabet getInstance(String theAlphabetXML) throws
XMLHandlingException {
return XMLUtils.parseAlphabetXML(theAlphabetXML);
}
public String toCXML() throws XMLHandlingException {
return XMLUtils.getAlphabetXML(this);
}
}
J'ai créé les méthodes utilitaires XMLUtil suivantes pour permettre le rassemblement et unmarshalling
public static String getAlphabetXML(Alph theAlphabet) throws XMLHandlingException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Writer writer = null;
try {
writer = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
} catch (IOException e) {
e.printStackTrace();
}
try {
JAXBContext JContext = JAXBContext.newInstance(Alphabet.class);
Marshaller JMarshaller = JContext.createMarshaller();
JMarshaller.marshal(theAlphabet, writer);
} catch (Throwable e) {
e.printStackTrace();
}
String theAlphabetXML = byteArrayOutputStream.toString();
return theAlphabetXML;
}
public static Alphabet parseAlphabetXML(String theAlphabetXML) throws
XMLHandlingException {
if(null == theAlphabetXML) {
return null;
}
try {
InputStream IPStream = new ByteArrayInputStream(theALphabetXML.getBytes());
JAXBContext JContext = JAXBContext.newInstance(Alphabet.class);
Unmarshaller JUnmarshaller = JContext.createUnmarshaller();
Alphabet alphabet = (Alphabet) JUnmarshaller.unmarshal(IPStream);
return alphabet;
} catch(Throwable t) {
t.printStackTrace();
}
}
Mon problème est que lorsque jamais j'essaye de marécher ou de marmotter je reçois l'erreur follower "javax.xml.bind.UnmarshalException: élément inattendu (a:" ", local:" Alphabet "). éléments attendus sont < {} Alphabet> »
Ai-je raté quelque chose? Toute aide est appréciée.
Merci
Mise à jour
Je suivais http://blog.bdoughan.com/2013/06/moxys-xmlvariablenode-using-maps-key-as.html et mis à jour mon code comme suit
demande et la réponse XML
<AlphabetReq>
<a>Apple</a>
<b>Ball</b>
<c>Cat</c>
<d>Dog</d>
<e>Elephant</e>
<f>Fox</f>
</AlphabetReq>
<AlphabetResp>
<a>Apple</a>
<b>Ball</b>
<c>Cat</c>
<d>Dog</d>
<e>Elephant</e>
<f>Fox</f>
</AlphabetResp>
AlphabetReq et les classes AlphabetResp
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Alph {
@XmlPath(".")
@XmlJavaTypeAdapter(AlphAdapter.class)
private LinkedHashMap<String, String> cLinkedHashMap = new
LinkedHashMap<String, String>();
@XmlPath(".")
@XmlJavaTypeAdapter(AlphAdapter.class)
private LinkedHashMap<String, String> gLinkedHashMap = new
LinkedHashMap<String, String>();
protected void put(String theKey, String theValue) {
cLinkedHashMap.put(theKey, theValue);
gLinkedHashMap.put(theKey, theValue);
}
protected String get(String theKey) {
return (String) cLinkedHashMap.get(theKey);
}
protected Set<Entry<String,String>> getCEntrySet() {
return cLinkedHashMap.entrySet();
}
protected Set<Entry<String,String>> getGEntrySet() {
return gLinkedHashMap.entrySet();
}
protected LinkedHashMap<String, String> getCLinkedHashMap() {
return cLinkedHashMap;
}
protected LinkedHashMap<String, String> getGLinkedHashMap() {
return gLinkedHashMap;
}
public String toCXML() throws XMLHandlingException {
return null;
}
public String toGXML() throws XMLHandlingException {
return null;
}
}
@XmlRootElement(name="AlphReq")
@XmlDiscriminatorValue("AlphabetReq")
@XmlAccessorType(XmlAccessType.FIELD)
public class AlphabetReq extends Alph {
public static AlphabetReq getInstance(String theAlphabetReqXML) throws
XMLHandlingException {
return XMLUtils.parseAlphabetReqXML(theAlphabetReqXML);
}
public String toCXML() throws XMLHandlingException {
return XMLUtils.getAlphabetReqXML(this);
}
}
@XmlRootElement(name="AlphResp")
@XmlDiscriminatorValue("AlphabetResp")
@XmlAccessorType(XmlAccessType.FIELD)
public class AlphabetResp extends Alph {
public static AlphabetResp getInstance(String theAlphabetRespXML) throws
XMLHandlingException {
return XMLUtils.parseAlphabetRespXML(theAlphabetRespXML);
}
public String toCXML() throws XMLHandlingException {
return XMLUtils.getAlphabetRespXML(this);
}
}
J'ai créé les éléments suivants utilitaires XMLUtil méthodes de triage et unmarshalling
public static String getAlphabetReqXML(Alph theAlphabet) throws XMLHandlingException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Writer writer = null;
try {
writer = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
} catch (IOException e) {
e.printStackTrace();
}
try {
JAXBContext JContext = JAXBContext.newInstance(AlphabetReq.class);
Marshaller JMarshaller = JContext.createMarshaller();
JMarshaller.marshal(theAlphabet, writer);
} catch (Throwable e) {
e.printStackTrace();
}
String theAlphabetReqXML = byteArrayOutputStream.toString();
return theAlphabetReqXML;
}
public static AlphabetReq parseAlphabetReqXML(String theAlphabetReqXML) throws
XMLHandlingException {
if(null == theAlphabetReqXML) {
return null;
}
try {
InputStream IPStream = new ByteArrayInputStream(theAlphabetReqXML.getBytes());
JAXBContext JContext = JAXBContext.newInstance(AlphabetReq.class);
Unmarshaller JUnmarshaller = JContext.createUnmarshaller();
AlphabetReq alphabetreq = (AlphabetReq) JUnmarshaller.unmarshal(IPStream);
return alphabetreq;
} catch(Throwable t) {
t.printStackTrace();
}
}
public static String getAlphabetRespXML(Alph theAlphabet) throws XMLHandlingException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Writer writer = null;
try {
writer = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
} catch (IOException e) {
e.printStackTrace();
}
try {
JAXBContext JContext = JAXBContext.newInstance(AlphabetResp.class);
Marshaller JMarshaller = JContext.createMarshaller();
JMarshaller.marshal(theAlphabet, writer);
} catch (Throwable e) {
e.printStackTrace();
}
String theAlphabetRespXML = byteArrayOutputStream.toString();
return theAlphabetRespXML;
}
public static AlphabetResp parseAlphabetReqXML(String theAlphabetRespXML) throws
XMLHandlingException {
if(null == theAlphabetRespXML) {
return null;
}
try {
InputStream IPStream = new ByteArrayInputStream(theAlphabetRespXML.getBytes());
JAXBContext JContext = JAXBContext.newInstance(AlphabetResp.class);
Unmarshaller JUnmarshaller = JContext.createUnmarshaller();
AlphabetResp alphabetresp = (AlphabetResp) JUnmarshaller.unmarshal(IPStream);
return alphabetresp;
} catch(Throwable t) {
t.printStackTrace();
}
}
et introduit une classe d'adaptateur
import java.util.*;
import java.util.Map.Entry;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.eclipse.persistence.oxm.annotations.XmlVariableNode;
public class AlphAdapter extends XmlAdapter<AlphAdapter.AdaptedMap, LinkedHashMap<String, String>>{
public static class AdaptedMap {
@XmlVariableNode("key")
List<AdaptedEntry> entries = new ArrayList<AdaptedEntry>();
}
public static class AdaptedEntry {
@XmlTransient
public String key;
@XmlValue
public String value;
}
@Override
public AdaptedMap marshal(LinkedHashMap<String, String> map) throws Exception {
AdaptedMap adaptedMap = new AdaptedMap();
for(Entry<String, String> entry : map.entrySet()) {
AdaptedEntry adaptedEntry = new AdaptedEntry();
adaptedEntry.key = entry.getKey();
adaptedEntry.value = entry.getValue();
adaptedMap.entries.add(adaptedEntry);
}
return adaptedMap;
}
@Override
public LinkedHashMap<String, String> unmarshal(AdaptedMap adaptedMap) throws Exception {
List<AdaptedEntry> adaptedEntries = adaptedMap.entries;
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(adaptedEntries.size());
for(AdaptedEntry adaptedEntry : adaptedMap.entries) {
map.put(adaptedEntry.key, adaptedEntry.value);
}
return map;
}
}
Lorsque j'exécute cela, je n'obtiens aucune valeur dans ma carte adaptée. il a des touches mais des valeurs que null par ex:
- clé: A, valeur: null
- clé: B, valeur: null
- clé: C, valeur: null clé
- : D , valeur: null
- clé: E, valeur: null
- clé: F, valeur: null
Toute aide est appréciée.
Merci
'catch (Throwable e) {return null; } '- ne fais pas ça. Cela rendra le débogage impossible – artbristol
Ce qui suit peut être ce que vous cherchez: http://blog.bdoughan.com/2013/06/moxys-xmlvariablenode-using-maps-key-as.html –
Bonjour @BlaiseDoughan, j'ai suivi votre lien et mis à jour mon code, il commet des erreurs. Toute aide est appréciée. merci – kvbrahmam