Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: for the plan.

Jump to: navigation, search



No metadata is required to convert your existing object model to XML. This example will demonstrate how easy it is to convert objects to XML using EclipseLink MOXy (JAXB). In later examples we will supply metadata to customize the XML representation.

Domain Model

For this example our domain model will represent customer information.

package example.gettingstarted;
import java.util.ArrayList;
import java.util.List;
public class Customer {
    private String name;
    private Address address;
    private List<PhoneNumber> phoneNumbers;
    public Customer() {
        phoneNumbers = new ArrayList<PhoneNumber>();
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public Address getAddress() {
        return address;
    public void setAddress(Address address) {
        this.address = address;
    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
package example.gettingstarted;
public class Address {
    private String street;
    private String city;
    public String getStreet() {
        return street;
    public void setStreet(String street) {
        this.street = street;
    public String getCity() {
        return city;
    public void setCity(String city) { = city;
package example.gettingstarted;
public class PhoneNumber {
    private String type;
    private String value;
    public String getType() {
        return type;
    public void setType(String type) {
        this.type = type;
    public String getValue() {
        return value;
    public void setValue(String value) {
        this.value = value;

Specifying the EclipseLink MOXy Runtime

You will need to have the JAXB APIs (included in Java SE 6) and eclipselink.jar (download site) on your classpath.

To specify that the EclipseLink MOXy (JAXB) runtime should be used you need to add a file called in the same package as the domain classes with the following entry.


Converting Objects to XML

The following code is used to convert the objects to XML.

package example.gettingstarted;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
public class Demo {
    public static void main(String[] args) throws JAXBException {
        // Step 1 - Create the Domain Model
        Customer customer = new Customer();
        customer.setName("Jane Doe");
        Address address = new Address();
        address.setStreet("123 Any Street");
        address.setCity("My Town");
        PhoneNumber workPhoneNumber = new PhoneNumber();
        PhoneNumber cellPhoneNumber = new PhoneNumber();
        // Step 2 - Convert the Domain Model to XML
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        JAXBElement<Customer> jaxbElement = new JAXBElement<Customer>(new QName(null, "customer"), Customer.class, customer);
        marshaller.marshal(jaxbElement, System.out);

XML Output

The following is the resulting XML. In the next example (JAXB Customizations) we will demonstrate how to control the format of the XML document using JAXB annotations.

<?xml version="1.0" encoding="UTF-8"?>
        <city>My Town</city>
        <street>123 Any Street</street>
    <name>Jane Doe</name>

Back to the top