JAXB @XmlRootElement annotation example

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

1. JAXB @XmlRootElement annotation type

@XmlRootElement maps a class or an enum type to an XML element. When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document.

@XmlRootElement annotation can be used with the following annotations: XmlType, XmlEnum, XmlAccessorType, XmlAccessorOrder.

1.1. Syntax

//Without name attribute
@XmlRootElement             //1

//With name attribute         
@XmlRootElement(name = "employee")                //2

2. JAXB @XmlRootElement example

Now see few examples of how using @XmlRootElement changes the XML representations.

2.1. @XmlRootElement with ‘name’ attribute

@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.FIELD)
public class EmployeeData implements Serializable {

  private static final long serialVersionUID = 1L;

  private Integer id;
  private String firstName;
  private String lastName;
}

Above converts to:

<employee>
    <id>1</id>
    <firstName>Lokesh</firstName>
    <lastName>Gupta</lastName>
</employee>

2.2. @XmlRootElement without ‘name’ attribute

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class EmployeeData implements Serializable {

  private static final long serialVersionUID = 1L;

  private Integer id;
  private String firstName;
  private String lastName;
}

Above converts to:

<EmployeeData>
    <id>1</id>
    <firstName>Lokesh</firstName>
    <lastName>Gupta</lastName>
</EmployeeData>

Drop me your questions in comments section.

Happy Learning !!

Reference : XmlRootElement 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.

5 thoughts on “JAXB @XmlRootElement annotation example”

  1. I’m returning a class’s object using @XmlRootElement annotation …. But at runtime it is giving

    HTTP Status 500 – Internal Server Error
    Type Status Report

    Message Internal Server Error

    Description The server encountered an unexpected condition that prevented it from fulfilling the request.

    while there is no output on console… HELP !!

    Reply
  2. How to avoid root element? For example:
    I have java classes Application and Person.

    class Application {
        Person person;
        String arbitraryComment;
    }
    class Person {
        String firstName;
        String lastName;
    }
    

    I get xml structure like this:

    <application>
                <person>
                   <firstName>?</firstName>
                   <lastName>?</lastName>              
                </person>
                <arbitraryComment>?</arbitraryComment>
    </application>
    

    And I would want xml structure to be:

    <application>
                 <firstName>?</firstName>
                 <lastName>?</lastName>              
                <arbitraryComment>?</arbitraryComment>
    </application>
    
    Reply

Leave a Comment

HowToDoInJava

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