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.
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 .
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.
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.
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
.
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;
}
}
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With