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

From Eclipsepedia

Jump to: navigation, search
(New page: ==Overview== This example will build upon the lessons learned in the previous example (JAXBCustomizations), and demonstrate ...)
 
(XML Output)
 
(5 intermediate revisions by one user not shown)
Line 1: Line 1:
 
==Overview==
 
==Overview==
This example will build upon the lessons learned in the previous example ([[EclipseLink/Examples/MOXy/GettingStarted/JAXBCustomizations|JAXBCustomizations]]), and demonstrate how MOXy extensions can be used to further customize the XML output.
+
 
 +
This example will build upon the lessons learned in the previous example ([[EclipseLink/Examples/MOXy/GettingStarted/JAXBCustomizations|JAXB Customizations]]), and demonstrate how MOXy extensions can be used to further customize the XML output.
 +
 
 +
==Using MOXy Extensions==
 +
 
 +
Some of the MOXy extensions are available through EclipseLink annotations, others require programmatic changes to the underlying metadata.  The EclipseLink annotation @XmlPath is used to specify path based mappings.
 +
 
 +
<source lang="java">
 +
package example.gettingstarted;
 +
 +
import java.util.ArrayList;
 +
import java.util.List;
 +
 +
import javax.xml.bind.annotation.XmlRootElement;
 +
import javax.xml.bind.annotation.XmlType;
 +
 
 +
import org.eclipse.persistence.oxm.annotations.XmlPath;
 +
 +
@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>();
 +
    }
 +
 +
    @XmlPath("personal-info/name/text()")
 +
    public String getName() {
 +
        return name;
 +
    }
 +
 +
    public void setName(String name) {
 +
        this.name = name;
 +
    }
 +
 +
    @XmlPath("contact-info/address")
 +
    public Address getAddress() {
 +
        return address;
 +
    }
 +
 +
    public void setAddress(Address address) {
 +
        this.address = address;
 +
    }
 +
 +
    @XmlPath("contact-info/phone-number")
 +
    public List<PhoneNumber> getPhoneNumbers() {
 +
        return phoneNumbers;
 +
    }
 +
 +
    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
 +
        this.phoneNumbers = phoneNumbers;
 +
    }
 +
 +
}
 +
</source>
 +
 
 +
==Converting Objects to XML==
 +
 
 +
The following code is used to convert the objects to XML.  This is the same code from the previous example ([[EclipseLink/Examples/MOXy/GettingStarted/JAXBCustomizations|JAXB Customizations]]).
 +
 
 +
<source lang="java">
 +
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);
 +
 
 +
    }
 +
 
 +
}
 +
</source>
 +
 
 +
==XML Output==
 +
 
 +
The following is the resulting XML.  In the next example ([[EclipseLink/Examples/MOXy/GettingStarted/ExternalizedMetadata|Externalized Metadata]]) we will demonstrate how use XML instead of annotations to represent the metadata.
 +
 
 +
 
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<customer>
 +
  <personal-info>
 +
      <name>Jane Doe</name>
 +
  </personal-info>
 +
  <contact-info>
 +
      <address>
 +
        <city>My Town</city>
 +
        <street>123 Any Street</street>
 +
      </address>
 +
      <phone-number type="work">613-555-1111</phone-number>
 +
      <phone-number type="cell">613-555-2222</phone-number>
 +
  </contact-info>
 +
</customer>
 +
</source>

Latest revision as of 14:51, 16 June 2010

Contents

[edit] Overview

This example will build upon the lessons learned in the previous example (JAXB Customizations), and demonstrate how MOXy extensions can be used to further customize the XML output.

[edit] Using MOXy Extensions

Some of the MOXy extensions are available through EclipseLink annotations, others require programmatic changes to the underlying metadata. The EclipseLink annotation @XmlPath is used to specify path based mappings.

package example.gettingstarted;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
import org.eclipse.persistence.oxm.annotations.XmlPath;
 
@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>();
    }
 
    @XmlPath("personal-info/name/text()")
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    @XmlPath("contact-info/address")
    public Address getAddress() {
        return address;
    }
 
    public void setAddress(Address address) {
        this.address = address;
    }
 
    @XmlPath("contact-info/phone-number")
    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }
 
    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }
 
}

[edit] Converting Objects to XML

The following code is used to convert the objects to XML. This is the same code from the previous example (JAXB Customizations).

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 (Externalized Metadata) we will demonstrate how use XML instead of annotations to represent the metadata.


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