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/ExternalizedMetadata"

(Converting Objects to XML)
Line 67: Line 67:
 
         // Step 2 - Convert the Domain Model to XML
 
         // Step 2 - Convert the Domain Model to XML
  
         JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
+
        Map<String, Source> metadata = new HashMap<String, Source>();
 +
        metadataSourceMap.put("example.gettingstarted", new StreamSource("eclipselink-oxm.xml"));
 +
 
 +
        Map<String, Object> properties = new HashMap<String, Object>();
 +
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadataSourceMap);
 +
 
 +
        Class[] classes = new Class[1];
 +
        classes[0] = Customer.class;
 +
 
 +
         JAXBContext jaxbContext = JAXBContext.newInstance(classes, properties);
  
 
         Marshaller marshaller = jaxbContext.createMarshaller();
 
         Marshaller marshaller = jaxbContext.createMarshaller();

Revision as of 14:49, 16 June 2010

Overview

This example will build upon the lessons learned in the previous example (MOXy Extensions), and demonstrate how the annotations can be expressed as XML. This technique is useful in situations where you are not able to modify the domain objects.

Externalized Metadata

The XML metadata below is equivalent to the metadata represented by the annotations used in the previous example.

<?xml version="1.0"?>
<xml-bindings 
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        version="2.1">
    <java-types>
        <java-type name="example.gettingstarted.Customer">
            <xml-root-element/>
            <xml-type prop-order="name address phoneNumbers"/>
            <java-attributes>
                <xml-element java-attribute="name" xml-path="personal-info/name/text()"/>
                <xml-element java-attribute="address" xml-path="contact-info/address"/>
                <xml-element java-attribute="phoneNumbers" xml-path="contact-info/phone-numbers"/>
            </java-attributes>
        </java-type>
        <java-type name="example.gettingstarted.PhoneNumber">
            <xml-root-element/>
            <java-attributes>
                <xml-attribute java-attribute="type"/>
                <xml-value java-attribute="number"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Converting Objects to XML

The following code is used to convert the objects to XML. Note how the JAXBContext is created using the externalized metadata.

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
 
        Map<String, Source> metadata = new HashMap<String, Source>();
        metadataSourceMap.put("example.gettingstarted", new StreamSource("eclipselink-oxm.xml"));
 
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadataSourceMap);
 
        Class[] classes = new Class[1];
        classes[0] = Customer.class;
 
        JAXBContext jaxbContext = JAXBContext.newInstance(classes, properties);
 
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
        marshaller.marshal(customer, System.out);
 
    }
 
}

Back to the top