JAXB @XmlElementWrapper Annotation Example

Java example of JAXB @XmlElementWrapper annotation in detail along with its usage during marshalling and unmarshalling operations.

1. @XmlElementWrapper Type

  1. This annotation generates a wrapper element around XML representation.
  2. This is primarily intended to be used to produce a wrapper XML element around collections.
  3. This annotation can be used with the following annotations: XmlElement, XmlElements, XmlElementRef, XmlElementRefs, XmlJavaTypeAdapter.
  4. 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

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

4 thoughts on “JAXB @XmlElementWrapper Annotation Example”

  1. Hi Lokesh,

    How to create Java Class from

    <SOAP-ENV:Body>
            <ns1:ppMitraInfoResponse xmlns:ns1="http://xxxxxx">
                <return>
                    <quota xsi:type="xsd:string">9071573</quota>
                    <Description xsi:type="xsd:string">DEVELMODE BY HOW</Description>
                </return>
            </ns1:ppMitraInfoResponse>
     </SOAP-ENV:Body>
    
    Reply
    • Full XML Response:

      <?xml version="1.0" encoding="UTF-8"?>
      <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
          <SOAP-ENV:Body>
              <ns1:ppMitraInfoResponse xmlns:ns1="http://xxxxxx">
                  <return>
                      <quota xsi:type="xsd:string">9071573</quota>
                      <Description xsi:type="xsd:string">DEVELMODE BY HOW</Description>
                  </return>
              </ns1:ppMitraInfoResponse>
          </SOAP-ENV:Body>
      </SOAP-ENV:Envelope>
      
      Reply
  2. 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?

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.