JAXB bind single XML element to Java object
in
Programming Questions
•
8 months ago
Hi,
I am having troubles binding a single element into an object with several attributes.
Here's what I have:
My XML (cannot be modified):
<type>
<name>some name</name>
<subtype1>aaaaaa</subtype1>
<subtype2>bbbbbb</subtype2>
</type>
My Java classes
@XmlRootElement(name = "type")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Type {
protected String name;
protected List<SubType> list;
// getters
@XmlElement(name = "name")
public String getName() {
return name;
}
@XmlElements({
@XmlElement(name = "subtype1", type = SubType.class),
@XmlElement(name = "subtype1", type = SubType.class)
})
public List<SubType> getList() {
return list;
}
}
@XmlAccessorType(XmlAccessType.PROPERTY)
public class SubType {
protected String defaultValue = "DEFAULT";
protected String subtype= null;
// getters
.....
}
I parse XML with Type class and get a correct value for name attribute.
However I cannot bind subtype1 and subtype2 nodes into SubType classes (subtype attribute is always empty).
Can someone please tell me if it's possible to bind an element to a class/object?
To modify classes to make it work ?
Thank you.
1