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.xml;

import org.kodejava.xml.support.Address;

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;

    @XmlElement
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}
package org.kodejava.xml.support;

public class Address {
    private String street;
    private String city;
    private String province;
    private String zipcode;
    private String country;

    // Getters & Setters
}
package org.kodejava.xml;

import org.kodejava.xml.support.Address;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class JAXBCustomRootElement {
    public static void main(String[] args) {
        Customer customer = new Customer();
        customer.setId(1);
        customer.setName("Johnny Mnemonic");

        Address address = new Address();
        address.setStreet("Sunset Road");
        address.setCity("Denpasar");
        address.setProvince("Bali");
        address.setZipcode("80225");
        address.setCountry("Indonesia");
        customer.setAddress(address);

        try {
            JAXBContext context = JAXBContext.newInstance(Customer.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(customer, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

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"?>
<cust>
    <id>1</id>
    <name>Johnny Mnemonic</name>
    <address>
        <city>Denpasar</city>
        <country>Indonesia</country>
        <province>Bali</province>
        <street>Sunset Road</street>
        <zipcode>80225</zipcode>
    </address>
</cust>

Maven Dependencies

<dependencies>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>    
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-ri</artifactId>
        <version>2.3.5</version>
        <type>pom</type>
    </dependency>
</dependencies>

Maven Central Maven Central

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.