I want marshall XMLAttribute in a derived class, but I have some problems.
I have 2 derived classes and 1 superclass as shown below:
public class Dog extends Animal { @XmlAttribute(name = "type") private String type; @XmlElement private String name; } public class Cat extends Animal { @XmlAttribute(name = "type") private String type; @XmlElement private String name; } @XmlSeeAlso({Dog.class, Cat.class}) public class Animal { } @XmlRootElement(name="some_element_wrapper") public SomeElementWrapper() { List<Animal> listAnimal; @XmlElement(name = "animals") public List<Animal> getListAnimal() {} public void setListAnimal(List<Animal> listAnimal) {} }
Suppose I have a list filled with some data. I want to generate XML from my classes as follows:
<some_element_wrapper> <animals> <animal type="dog">....</animal> <animal type="cat">....</animal> </animals> </some_element_wrapper>
My problem is that I get what I want, except for the type attribute. I tried using other solutions, moving the attribute type to the superclass or overriding the derived field, but with no result. Please any suggestion?
source share