Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use @XmlElement and @XmlRootElement for marshalling object inside an object?

Tags:

java

xml

jaxb

I have seen many implementations of JAXB where we can convert java primitive to XML element using @XmlElement annotation.

But, I want to convert following POJO to XML (Note there is an address object inside employee class and not just primitives):

public class Employee {
    private Address address;
    private int employeeId;

    // constructors + setters + getters
}

How to use these JAXB annotations to marshall an employee object to XML?

Thanks.

like image 700
Akshay Lokur Avatar asked May 12 '14 05:05

Akshay Lokur


People also ask

What is the use of @XmlRootElement annotation?

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. This annotation can be used with the following annotations: XmlType , XmlEnum , XmlAccessorType , XmlAccessorOrder .

How does JAXB marshalling work?

In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.

What is marshalling and Unmarshalling in Java example?

Marshalling is the process of transforming Java objects into XML documents. Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API. It provides API for marshalling, unmarshalling and validating.


1 Answers

There is nothing different you need to do to marshal a POJO property from what you do to marshal a primitive property. The referenced POJO class does not need to be annotated with @XmlRootElement.

Java Model

Employee

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Employee {

    private Address address;
    private int employeeId;

    public Address getAddress() {
        return address;
    }

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

    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

}

Address

There is nothing special that you need to do to have Address marshalled as part of Employee.

public class Address {

    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

}

Demo Code

Below is some demo code that will populate and employee model and marshal it to XML.

Demo

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Employee.class);

        Address address = new Address();
        address.setStreet("1 A Street");

        Employee employee = new Employee();
        employee.setEmployeeId(123);
        employee.setAddress(address);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(employee, System.out);
    }

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <address>
        <street>1 A Street</street>
    </address>
    <employeeId>123</employeeId>
</employee>

Renaming the Marshalled Element

If you want to override the default element name then you can use the @XmlElement annotation regardless of what type the property is.

Employee

import javax.xml.bind.annotation.*;

@XmlRootElement
public class Employee {

    private Address address;
    private int employeeId;

    @XmlElement(name="ADDR")
    public Address getAddress() {
        return address;
    }

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

    @XmlElement(name="ID")
    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <ADDR>
        <street>1 A Street</street>
    </ADDR>
    <ID>123</ID>
</employee>
like image 200
bdoughan Avatar answered Oct 17 '22 04:10

bdoughan