Question

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.

Was it helpful?

Solution 2

I was able to achieve "object inside object" XML marshalling with JAXB by following appraoch given below (i.e. by annotating both the classes with @XmlRootElement annotation):

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

@XmlRootElement
public class Employee {
    @XmlElement
    private Address address;
     .
     .
}

@XmlRootElement
public class Address {
    .
    .
}

OTHER TIPS

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>

Using Jaxb you can try the following code. Alternatively you may try Xstream

  @XmlRootElement
public class TestObject {

    String a;

    TestObject1 anotherObject;
    public String getA() {
        return a;
    }
    @XmlElement
    public void setA(String a) {
        this.a = a;
    }


    public TestObject1 getAnotherObject() {
        return anotherObject;
    }
    @XmlElement
    public void setAnotherObject(TestObject1 anotherObject) {
        this.anotherObject = anotherObject;
    }
    public static void main(String[] args) throws JAXBException {
        TestObject object = new TestObject();
        object.setA("A");
        TestObject1 anotherObject = new TestObject1();
        anotherObject.setB("B");
        object.setAnotherObject(anotherObject);
        File file = new File("output.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(TestObject.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(object, file);
        jaxbMarshaller.marshal(object, System.out);
    }
}

========================

@XmlRootElement
public class TestObject1 {

    String b;

    public String getB() {
        return b;
    }

    @XmlElement
    public void setB(String b) {
        this.b = b;
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top