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

From Eclipsepedia

Jump to: navigation, search
(XML Output)
(Customizing a Property)
 
Line 69: Line 69:
  
 
     private String type;
 
     private String type;
     private String number;
+
     private String value;
  
 
     @XmlAttribute
 
     @XmlAttribute
Line 82: Line 82:
 
     @XmlValue
 
     @XmlValue
 
     public String getValue() {
 
     public String getValue() {
         return number;
+
         return value;
 
     }
 
     }
  
 
     public void setValue(String value) {
 
     public void setValue(String value) {
         this.number = value;
+
         this.value = value;
 
     }
 
     }
  

Latest revision as of 13:28, 16 June 2010

Contents

[edit] Overview

This example will build upon the lessons learned in the previous example (The Basics), and demonstrate how JAXB annotations can be used to customize the XML output.

[edit] Customizing a Type

In the previous example (The Basics) we required a supplementary object to serve as the root of our model. When can use JAXB annotations to specify which classes in our domain model can serve as roots, this is done using the @XmlRootElement annotation. Now that this annotation has been added we no longer require the use of the JAXBElement class.

By default the order in which the XML elements are marshalled out is dependent on the implementation. We can control the order using the propOrder property on the JAXB annotation @XmlType.

package example.gettingstarted;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
@XmlRootElement
@XmlType(propOrder={"name", "address", "phoneNumbers"})
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;
    }
 
}

[edit] Customizing a Property

By default Java properties are mapped to XML elements. In order to map a property to an XML attribute the JAXB annotation @XmlAttribute is used. If you want to map a property directly to a text field without an element wrapper simply use the JAXB annotation @XmlValue.

package example.gettingstarted;
 
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
 
public class PhoneNumber {
 
    private String type;
    private String value;
 
    @XmlAttribute
    public String getType() {
        return type;
    }
 
    public void setType(String type) {
        this.type = type;
    }
 
    @XmlValue
    public String getValue() {
        return value;
    }
 
    public void setValue(String value) {
        this.value = value;
    }
 
}

[edit] Converting Objects to XML

The following code is used to convert the objects to XML. Note that unlike in the previous example (The Basics) we do not need to wrap the Customer object in a JAXBElement.

package example.gettingstarted;
 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
 
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);
 
        marshaller.marshal(customer, System.out);
 
    }
 
}

[edit] XML Output

The following is the resulting XML. In the next example (MOXy Extensions) we will demonstrate how to further control the format of the XML document using MOXy Extensions.

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