Difference between revisions of "EclipseLink/Examples/MOXy/Dynamic/StaticComparison"

From Eclipsepedia

< EclipseLink‎ | Examples‎ | MOXy‎ | Dynamic
Jump to: navigation, search
(New page: ==Overview== In this example we will compare dynamic JAXB to the more familiar static form. The main thing to note is that the use of JAXB is almost identical, the main difference is usin...)
 
(Dynamic JAXB)
Line 35: Line 35:
 
marshaller.marshal(customer, System.out);
 
marshaller.marshal(customer, System.out);
 
</source>
 
</source>
 +
 +
==Next Steps==
 +
Next we'll dig deeper into how the Dynamic JAXB Context is bootstrapped and you can use it:  [[EclipseLink/Examples/MOXy/Dynamic/JAXBContextFromXMLSchema|Dynamic JAXB Context from XML Schema]]

Revision as of 11:14, 24 June 2010

Contents

Overview

In this example we will compare dynamic JAXB to the more familiar static form. The main thing to note is that the use of JAXB is almost identical, the main difference is using the domain model.

Static JAXB

We will bootstrap our JAXBContext on annotated classes generated from an XML schema. If the XML schema were to change we would need to regenerate the classes.

JAXBContext jaxbContext = JAXBContext.newInstance(org.example.Customer.class);
 
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) unmarshaller.unmarshal(inputStream);
 
Address address = new Address();
address.setStreet("123 A Street");
address.setCity("Any Town");
customer.setAddress(address);
 
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(customer, System.out);

Dynamic JAXB

Using dynamic JAXB we will bootstrap our JAXBContext on hte XML schema itself. If the XML schema were to change, we would not need to regenerate any classes.

DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);
 
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(inputStream);
 
DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
address.set(street, "123 A Street");
address.set(city, "Any Town");
customer.set("address", address);
 
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(customer, System.out);

Next Steps

Next we'll dig deeper into how the Dynamic JAXB Context is bootstrapped and you can use it: Dynamic JAXB Context from XML Schema