HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / JAXB / JAXB @XmlRootElement annotation example

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

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. NAMAN GOYAL

    July 9, 2020

    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 !!

    • Lokesh Gupta

      July 10, 2020

      Please share the stack trace.

  2. surabhi priya

    September 16, 2019

    If i want a ArrayList in my XML how should we do that ?

  3. Johnny

    March 21, 2019

    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>
    
    • Lokesh Gupta

      March 21, 2019

      Here root node is “application”. It is not possible (also not advisable) to avoid “person” node.

Comments are closed on this article!

Search Tutorials

JAXB Tutorial

  • JAXB – Annotations
  • JAXB – @XmlRootElement
  • JAXB – @XmlElementWrapper
  • JAXB – Marshaller
  • JAXB – Unmarshaller
  • JAXB – Convert XML to Java Object
  • JAXB – Convert JSON to Java Object
  • JAXB – Convert Java Object to XML
  • JAXB – Convert Java Object to JSON
  • JAXB – Map
  • JAXB – List and Set
  • JAXB – Generate Schema
  • JAXB – Schema Validation
  • JAXB – JAXBException
  • JAXB – IllegalAnnotationExceptions
  • JAXB – Marshal without @XmlRootElement
  • JAXB – Unmarshal without @XmlRootElement
  • Jackson 2 – Object from/to JSON
  • Jackson – Object from/to json
  • Jackson – JSON from/to Map

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)