EclipseLink/Examples/MOXy/JAXB/Runtime

From Eclipsepedia

< EclipseLink‎ | Examples‎ | MOXy‎ | JAXB
Jump to: navigation, search

Contents

Overview

The following example will demonstrate how to use EclipseLink MOXy's JAXB functionality to:

  • Unmarshal an XML Document
  • Modify the XML data using the interfaces generated by the compiler
  • Marshal the modified data to XML

Creating a JAXBContext using jaxb.properties

In our example, we create the JAXBContext as follows. A jaxb.properties file indicating the EclipseLink JAXB runtime must be available on the classpath in the corresponding package:

JAXBContext jaxbContext = JAXBContext.newInstance("org.example.customer_example");

An alternate way to create a new JAXBContext is to supply an array of Class objects. Again, the jaxb.properties file must be available on the classpath in the corresponding package:

Class[] classes = new Class[4]; 
classes[0] = org.example.customer_example.AddressType.class; 
classes[1] = org.example.customer_example.ContactInfo.class; 
classes[2] = org.example.customer_example.CustomerType.class; 
classes[3] = org.example.customer_example.PhoneNumber.class; 
JAXBContext jaxbContext = JAXBContext.newInstance(classes);

Unmarshalling the XML Document

With a JAXBContext created, we can now unmarshal an XML document using the statically generated classes:

File file = new File("Customer-data.xml");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StreamSource source = new StreamSource(file);
JAXBElement customerElement = unmarshaller.unmarshal(source, CustomerType.class);
CustomerType customer = (CustomerType) customerElement.getValue();

Modify the Objects

Below are examples of manipulating the XML data using the JAXB-generated static classes. Note that there are JavaBean-type accessors on the static interfaces.

Modifying the ShippingAddress ZipCode:

customer.getContactInfo().getShippingAddress().setZipCode("27601");

Adding a new PhoneNumber:

PhoneNumber homePhoneNumber  = new ObjectFactory().createPhoneNumber();
homePhoneNumber.setType("home");
homePhoneNumber.setValue("(613) 555-3333");
customer.getContactInfo().getPhoneNumber().add(homePhoneNumber);

Note the use of the ObjectFactory class to create a new PhoneNumber object. In addition to generating the model classes used in this example, the JAXB compiler also generates an ObjectFactory class that can be used to create any of the generated types.

Removing all "cell" PhoneNumbers:

ArrayList phoneNumbersToRemove = new ArrayList();
List phoneNumbers = customer.getContactInfo().getPhoneNumber();
Iterator it = phoneNumbers.iterator();
while (it.hasNext()) {
    PhoneNumber phoneNumber = (PhoneNumber) it.next();
    if (phoneNumber.getType().equals("cell")) {
        phoneNumbersToRemove.add(phoneNumber);
    }
}
phoneNumbers.removeAll(phoneNumbersToRemove);

Marshalling the Objects to XML

The following code segment demonstrates how to marshal Customer objects back to XML. In this example the stream we are saving to is System.out, so the XML text will be printed to the console.

Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(customerElement, System.out);