I have 2 derived classes and 1 super class, as shown below Show:
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() {
ListlistAnimal;
@XmlElement(name = "animals")
public ListgetListAnimal() {}
public void setListAnimal(ListlistAnimal) {}
}
Suppose I have a List filled with some data.
I want to generate XML from my class as follows:
....
....
My question is, In addition to the type attribute, I got what I wanted. I tried other different solutions, moving the attribute type in the super class, or overwriting the derived type field, but no results.
Excuse me, any suggestions?
JAXB (JSR-222) The implementation will not automatically recognize the mapped class subclasses. You need to include them in the class array used to bootstrap the JAXBContext, or use the @XmlSeeAlso annotation on one of the mapped classes.
@XmlSeeAlso(Dog.class, Cat.class)
public class Animal {
}
Inheritance indicators p>
If you want to use the type attribute to specify the subtype being used, I suggest not to do this and use the xsi:type attribute instead of using XML (XML and XML Schema) in XML to indicate the way of inheritance and default It is expressed in JAXB.
> http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html
If you really don’t want to use xsi: type attribute, you can use XmlAdapter to use type attribute as inheritance indicator.
> http://blog.bdoughan.com/2012/01/jaxb-and-inhertiance-using-xmladapter. html
EclipseLink JAXB (MOXy) also provides an extension (@ XmlDescrinatorNode / @ XmlDescrimatorValue) to make this use case easier (I am the leader of MOXy).
> http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-moxy-extension.html
I want to be in a derived class Marshalling XMLAttribute, but I have some problems.
I have 2 derived classes and 1 super class, as shown below:
public class Dog extends Animal {
@XmlAttr ibute(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() {
ListlistAnimal;
@XmlElement(name = "animals")
public ListgetListAnimal() {}
public void setListAnimal(ListlistAnimal) {}
}
Suppose I have a List filled with some data.< br>I want to generate XML from my class as follows:
....
....
< p>My problem is that in addition to the type attribute, I get what I want. I tried other different solutions, moving the attribute type in the super class, or overwriting the derived type field, but No results.
Excuse me, any suggestions?
Make JAXBContext aware of subclasses
JAXB (JSR-222) will not be implemented Automatically identify mapped class subclasses. You need to include them in the class array used to bootstrap JAXBContext, or use the @XmlSeeAlso annotation on one of the mapped classes.
@ XmlSeeAlso(Dog.class, Cat.class)
public class Animal {
}
Inheritance indicators
If you want to use the type attribute to Specify the subtype being used. I suggest not to do this and use the xsi:type attribute instead of using XML in XML (XML and XML Schema) to indicate the way of inheritance and the default representation in JAXB.
> http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html
If you really don’t want to use the xsi:type attribute, you can use XmlAdapter to set the type attribute Used as an inheritance indicator.
> http://blog.bdoughan.com/2012/01/jaxb-and-inhertiance-using-xmladapter.html
EclipseLink JAXB (MOXy) also provides an extension (@ XmlDescrinatorNode / @ XmlDescrimatorValue) to make this use case easier (I am the leader of MOXy).
> http://blog.bdoughan. com/2010/11/jaxb-and-inheritance-moxy-extension.html