I need to change List to Map in JAXB unmarshalling file.
The xml file
<jrx:person>
<jrx:ulement name="id" type="Integer" value="1"/>
<jrx:ulement name="name" type="String" value="neps"/>
</jrx:person>
The java classes,
@XmlRootElement(name = "person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
@XmlElement(name = "ulement")
private List<Property> props;
}
@XmlRootElement(name = "ulement")
@XmlAccessorType(XmlAccessType.FIELD)
public class Property {
@XmlAttribute
protected String name;
@XmlAttribute
private String type;
@XmlAttribute
private String value;
}
I have to change the class implementation to
@XmlRootElement(name = "person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
@XmlElement(name = "ulement")
**private Map<String, Property> props;**
}
So that i ll be able to fetch the property using the keys quickly. Please suggest me some implementation to make it work.
I have done the following changes but still the values are not mapped,
The classes,
@XmlRootElement(name = "person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
@XmlJavaTypeAdapter(value = PropertyAdapter.class)
private Map<String, Property> map;
}
public class PropertyAdapter extends XmlAdapter<Properties, Map<String, Property>> {
@Override
public Properties marshal(Map<String, Property> arg0) throws Exception {
return null;
}
@Override
public Map<String, Property> unmarshal(Properties p) throws Exception {
Map<String, Property> map = new HashMap<String, Property>();
for (Property entry : p.props) {
map.put(entry.name, entry);
}
return map;
}
}
public class Properties {
@XmlElement(name = "ulement")
public List<Property> props;
}
But the values r Null at ,
@XmlRootElement(name = "person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
**@XmlJavaTypeAdapter(value = PropertyAdapter.class)
private Map<String, Property> map;**
}
What exactly is missing over there.
, Map>, then getting the following error : - com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.util.List is an interface, and JAXB can't handle interfaces.
– user3095047 Aug 24 '16 at 12:53