Java example of JAXB @XmlElementWrapper annotation in detail along with its usage during marshalling and unmarshalling operations.
1. @XmlElementWrapper Type
- This annotation generates a wrapper element around XML representation.
- This is primarily intended to be used to produce a wrapper XML element around collections.
- This annotation can be used with the following annotations:
XmlElement
,XmlElements
,XmlElementRef
,XmlElementRefs
, XmlJavaTypeAdapter. - The
@XmlElementWrapper
annotation can be used with the following program elements:- JavaBean property
- non static, non transient field
2. JAXB @XmlElementWrapper Example
Now see few examples of how using @XmlElementWrapper
along with @XmlElement
changes the XML representations.
2.1. Use @XmlElementWrapper and @XmlElement (Wrapped collection)
@XmlRootElement(name = "employee") @XmlAccessorType(XmlAccessType.FIELD) public class Employee implements Serializable { private static final long serialVersionUID = 1L; @XmlElementWrapper(name="hobbies") @XmlElement(name="hobby") private List<String> hobbies; private Integer id; private String firstName; private String lastName; }
Above converts to:
<employee> <id>1</id> <firstName>Lokesh</firstName> <lastName>Gupta</lastName> <hobbies> <hobby>Swimming</hobby> <hobby>Playing</hobby> <hobby>Karate</hobby> </hobbies> </employee>
2.2. Use Only @XmlElementWrapper
@XmlRootElement(name = "employee") @XmlAccessorType(XmlAccessType.FIELD) public class Employee implements Serializable { private static final long serialVersionUID = 1L; @XmlElementWrapper(name="hobbies") //@XmlElement(name="hobby") //Comment it out private List<String> hobbies; private Integer id; private String firstName; private String lastName; }
Above converts to:
<employee> <id>1</id> <firstName>Lokesh</firstName> <lastName>Gupta</lastName> <hobbies> <hobbies>Swimming</hobbies> <hobbies>Playing</hobbies> <hobbies>Karate</hobbies> </hobbies> </employee>
2.3. Do not use @XmlElementWrapper (Unwrapped collection)
@XmlRootElement(name = "employee") @XmlAccessorType(XmlAccessType.FIELD) public class Employee implements Serializable { private static final long serialVersionUID = 1L; //@XmlElementWrapper(name="hobbies") //Comment it out @XmlElement(name="hobby") private List<String> hobbies; private Integer id; private String firstName; private String lastName; }
Above converts to:
<employee> <id>1</id> <firstName>Lokesh</firstName> <lastName>Gupta</lastName> <hobby>Swimming</hobby> <hobby>Playing</hobby> <hobby>Karate</hobby> </employee>>
Drop me your questions in comments section.
Happy Learning !!
Reference : XmlElementWrapper Java Doc
Exclente explantion, regards from Argentina.
Hi Lokesh,
How to create Java Class from
Full XML Response:
I have a similar implementation – I get JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT
@XmlElementWrapper(name=”streetLines”)
@XmlElement(name=”streetLine”)
private List streetLines;
Anything wrong here?