Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MOXy/GettingStarted/TheBasics"

(Domain Model)
(Domain Model)
Line 101: Line 101:
  
 
}
 
}
 +
</source>
 +
 +
==Converting Objects to XML==
 +
 +
The following code is used to convert the objects to XML.
 +
 +
<source lang="java">
 +
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");
 +
        customer.setAddress(address);
 +
 +
        PhoneNumber workPhoneNumber = new PhoneNumber();
 +
        workPhoneNumber.setType("work");
 +
        workPhoneNumber.setValue("613-555-1111");
 +
        customer.getPhoneNumbers().add(workPhoneNumber);
 +
 +
        PhoneNumber cellPhoneNumber = new PhoneNumber();
 +
        cellPhoneNumber.setType("cell");
 +
        cellPhoneNumber.setValue("613-555-2222");
 +
        customer.getPhoneNumbers().add(cellPhoneNumber);
 +
 +
        // 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);
 +
 +
    }
 +
 +
}
 +
</source>
 +
 +
==XML Output==
 +
 +
The following is the resulting XML.
 +
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 +
<customer>
 +
    <address>
 +
        <city>My Town</city>
 +
        <street>123 Any Street</street>
 +
    </address>
 +
    <name>Jane Doe</name>
 +
    <phoneNumbers>
 +
        <type>work</type>
 +
        <value>613-555-1111</value>
 +
    </phoneNumbers>
 +
    <phoneNumbers>
 +
        <type>cell</type>
 +
        <value>613-555-2222</value>
 +
    </phoneNumbers>
 +
</customer>
 
</source>
 
</source>

Revision as of 15:01, 8 January 2010

Overview

This example will demonstrate how easy it is to convert objects to XML using EclipseLink MOXy (JAXB).

Domain Model

For this example our domain model will represent customer information.

package example.model;
 
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) {
        this.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.model;
 
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) {
        this.city = city;
    }
 
}
package example.model;
 
public class PhoneNumber {
 
    private String type;
    private String number;
 
    public String getType() {
        return type;
    }
 
    public void setType(String type) {
        this.type = type;
    }
 
    public String getValue() {
        return number;
    }
 
    public void setValue(String value) {
        this.number = value;
    }
 
}

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");
        customer.setAddress(address);
 
        PhoneNumber workPhoneNumber = new PhoneNumber();
        workPhoneNumber.setType("work");
        workPhoneNumber.setValue("613-555-1111");
        customer.getPhoneNumbers().add(workPhoneNumber);
 
        PhoneNumber cellPhoneNumber = new PhoneNumber();
        cellPhoneNumber.setType("cell");
        cellPhoneNumber.setValue("613-555-2222");
        customer.getPhoneNumbers().add(cellPhoneNumber);
 
        // 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.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
    <address>
        <city>My Town</city>
        <street>123 Any Street</street>
    </address>
    <name>Jane Doe</name>
    <phoneNumbers>
        <type>work</type>
        <value>613-555-1111</value>
    </phoneNumbers>
    <phoneNumbers>
        <type>cell</type>
        <value>613-555-2222</value>
    </phoneNumbers>
</customer>

Copyright © Eclipse Foundation, Inc. All Rights Reserved.