Skip to main content
Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MOXy/JaxbRuntime"

m (Create the JAXB Context (Metadata))
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
== Create the JAXB Context (Metadata) ==
 
== Create the JAXB Context (Metadata) ==
The first thing that needs to be done in an JAXB application is to create the JAXBContext.
+
The first thing that needs to be done in an JAXB application is to create the JAXBContext.  To ensure the EclipseLink JAXB runtime is used see [[EclipseLink/Examples/MOXy/JaxbProperties | Specifying the EclipseLink JAXB Runtime]].
  
 
'''Code Example'''
 
'''Code Example'''
<br>Class[] classes = new Class[4];
+
Class[] classes = new Class[4];
<br>classes[0] = AddressType.class;
+
classes[0] = AddressType.class;
<br>classes[1] = ContactInfo.class;
+
classes[1] = ContactInfo.class;
<br>classes[2] = CustomerType.class;
+
classes[2] = CustomerType.class;
<br>classes[3] = PhoneNumber.class;
+
classes[3] = PhoneNumber.class;
<br>JAXBContext jaxbContext = JAXBContext.newInstance(classes);
+
JAXBContext jaxbContext = JAXBContext.newInstance(classes);
 +
 
 +
 
 +
See ''[http://www.eclipse.org/eclipselink/documentation/ Developing JAXB Applications Using EclipseLink MOXy]'' for details.
  
 
== Unmarshal the XML Document ==
 
== Unmarshal the XML Document ==
JAXB is used to manipulate XML data.  The following code demonstrates how to unmarshal an XML document using the statically generated classes see [[EclipseLink/Examples/SDO/SdoClassGen | Generating Java Source from an XML Schema]].
+
JAXB is used to manipulate XML data.  The following code demonstrates how to unmarshal an XML document using the statically generated classes see [[EclipseLink/Examples/MOXy/JaxbClassGen | Generating Java Source from an XML Schema]].
  
 
'''Code Example'''
 
'''Code Example'''
Line 21: Line 24:
  
 
== Modify the Objects ==
 
== Modify the Objects ==
Below is an example of manipulating the Objects.
+
Below is an example of manipulating the objects.
  
 
'''Code Example'''
 
'''Code Example'''
Line 30: Line 33:
 
 
 
== Marshal the Objects ==
 
== Marshal the Objects ==
The following code segment demonstrates how to marshal the Objects back to XML.
+
The following code segment demonstrates how to marshal the objects back to XML.
  
 
'''Code Example'''
 
'''Code Example'''

Latest revision as of 12:30, 30 January 2013

Create the JAXB Context (Metadata)

The first thing that needs to be done in an JAXB application is to create the JAXBContext. To ensure the EclipseLink JAXB runtime is used see Specifying the EclipseLink JAXB Runtime.

Code Example

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


See Developing JAXB Applications Using EclipseLink MOXy for details.

Unmarshal the XML Document

JAXB is used to manipulate XML data. The following code demonstrates how to unmarshal an XML document using the statically generated classes see Generating Java Source from an XML Schema.

Code Example
File file = new File("../Resource/bin/Customer-data.xml");
StreamSource source = new StreamSource(file);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement jaxbElement = unmarshaller.unmarshal(source, CustomerType.class);
CustomerType customer = (CustomerType) jaxbElement.getValue();

Modify the Objects

Below is an example of manipulating the objects.

Code Example
PhoneNumber homePhoneNumber = new PhoneNumber();
homePhoneNumber.setNumberType("home");
homePhoneNumber.setValue("(613) 555-3333");
customer.getContactInfo().getPhoneNumber().add(homePhoneNumber);

Marshal the Objects

The following code segment demonstrates how to marshal the objects back to XML.

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

Back to the top