How do I change the XML root element name in JAXB?

In the following code snippet you will learn how to change the default root element name of the XML generated by the JAXB API. By default the name of the class is use as the root element name. To change the root element name we can use the name property of the @XmlRootElement annotation. In the Customer model below we change the root element name into cust.

package org.kodejava.example.jaxb;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "cust")
@XmlType(propOrder = {"id", "name", "address"})
public class Customer {
    private Integer id;
    private String name;
    private Address address;

    public Integer getId() {
        return id;

    public void setId(Integer id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public Address getAddress() {
        return address;

    public void setAddress(Address address) {
        this.address = address;

When we convert this POJO to XML using JAXB API we will get the following result:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <name>Johnny Mnemonic</name>
        <street>Sunset Road</street>
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.
Wayan Saryada

Leave a Reply