How to unmarshal xml to map



I have xml:



<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Environments>
<Environment Name="A" URIPath="http://a.com" />
<Environment Name="B" URIPath="http://b.com" />
</Environments>


I want to map this xml to Map where first element in the tuple will be Name so I could search by it. I have classes:


Env



@XmlRootElement(name = "Environments")
@XmlAccessorType(XmlAccessType.FIELD)
public class Env {

String Name;
String URIPath;

public String getName() {
return Name;
}
@XmlAttribute
public void setName(String name) {
Name = name;
}

public String getURIPath() {
return URIPath;
}
@XmlAttribute
public void setURIPath(String URIPath) {
this.URIPath = URIPath;
}
}


EnvMap



@XmlRootElement(name = "Environments")
@XmlAccessorType(XmlAccessType.FIELD)
public class EnvMap {

public Map<String, Env> getEnvMap() {
return envMap;
}

public void setEnvMap(Map<String, Env> envMap) {
this.envMap = envMap;
}

private Map<String, Env> envMap = new HashMap<String, Env>();

}


App



...
JAXBContext jaxbContext = JAXBContext.newInstance(EnvMap.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
envMap = (EnvMap) jaxbUnmarshaller.unmarshal( inputStream );

for(String name : envMap.getEnvMap().keySet()){
System.out.println(envMap.getEnvMap().get(name).getURIPath());
System.out.println(envMap.getEnvMap().get(name).getURIPath());
}
...


I am getting:



com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "URIPath"
this problem is related to the following location:
at public java.lang.String app.model.Env.getURIPath()
at app.model.Env
at private java.util.Map app.model.EnvMap.envMap
at app.model.EnvMap
this problem is related to the following location:
at java.lang.String app.model.Env.URIPath
at app.model.Env
at private java.util.Map app.model.EnvMap.envMap
at app.model.EnvMap


How to fix it?


No comments:

Post a Comment