Jump to: navigation, search

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

Line 18: Line 18:
 
eclipselink.jlib=C:/EclipseLink-1.0/eclipselink/jlib
 
eclipselink.jlib=C:/EclipseLink-1.0/eclipselink/jlib
 
...
 
...
</source>
+
</pre>
  
 
You can compile and run the Example at any time by typing <tt>ant</tt> from the Example directory.   
 
You can compile and run the Example at any time by typing <tt>ant</tt> from the Example directory.   
Line 51: Line 51:
 
The standard way to specify which JAXB implementation should be used is through a file called <tt>jaxb.properties</tt>, which contains a single property, <tt>javax.xml.bind.context.factory</tt>. This file must be available on the classpath in the corresponding package (in this example, "<tt>org.example.customer_example</tt>").  To specify that the EclipseLink JAXB implementation should be used, your <tt>jaxb.properties</tt> file should have the following content:
 
The standard way to specify which JAXB implementation should be used is through a file called <tt>jaxb.properties</tt>, which contains a single property, <tt>javax.xml.bind.context.factory</tt>. This file must be available on the classpath in the corresponding package (in this example, "<tt>org.example.customer_example</tt>").  To specify that the EclipseLink JAXB implementation should be used, your <tt>jaxb.properties</tt> file should have the following content:
  
<source lang="java">
+
<pre>
 
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
 
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
</source>
+
</pre>
  
 
In our example, we create the JAXBContext as follows:
 
In our example, we create the JAXBContext as follows:
  
<source lang="java">
+
<pre>
 
JAXBContext jaxbContext = JAXBContext.newInstance("org.example.customer_example");
 
JAXBContext jaxbContext = JAXBContext.newInstance("org.example.customer_example");
</source>
+
</pre>
  
 
An alternate way to create a new JAXBContext is to supply an array of Class objects.  Again, the <tt>jaxb.properties</tt> file must be available on the classpath in the corresponding package:
 
An alternate way to create a new JAXBContext is to supply an array of Class objects.  Again, the <tt>jaxb.properties</tt> file must be available on the classpath in the corresponding package:
  
<source lang="java">
+
<pre>
 
Class[] classes = new Class[4];  
 
Class[] classes = new Class[4];  
 
classes[0] = org.example.customer_example.AddressType.class;  
 
classes[0] = org.example.customer_example.AddressType.class;  
Line 70: Line 70:
 
classes[3] = org.example.customer_example.PhoneNumber.class;  
 
classes[3] = org.example.customer_example.PhoneNumber.class;  
 
JAXBContext jaxbContext = JAXBContext.newInstance(classes);
 
JAXBContext jaxbContext = JAXBContext.newInstance(classes);
</source>
+
</pre>
  
 
==Unmarshalling the XML Document==
 
==Unmarshalling the XML Document==
Line 76: Line 76:
 
With a JAXBContext created, we can now unmarshal an XML document using the statically generated classes:
 
With a JAXBContext created, we can now unmarshal an XML document using the statically generated classes:
  
<source lang="java">
+
<pre>
 
File file = new File("Customer-data.xml");
 
File file = new File("Customer-data.xml");
 
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
 
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Line 82: Line 82:
 
JAXBElement customerElement = unmarshaller.unmarshal(source, CustomerType.class);
 
JAXBElement customerElement = unmarshaller.unmarshal(source, CustomerType.class);
 
CustomerType customer = (CustomerType) customerElement.getValue();
 
CustomerType customer = (CustomerType) customerElement.getValue();
</source>
+
</pre>
  
 
==Modify the Objects==
 
==Modify the Objects==
Line 90: Line 90:
 
Modifying the ShippingAddress ZipCode:
 
Modifying the ShippingAddress ZipCode:
  
<source lang="java">
+
<pre>
 
customer.getContactInfo().getShippingAddress().setZipCode("27601");
 
customer.getContactInfo().getShippingAddress().setZipCode("27601");
</source>
+
</pre>
  
 
Adding a new PhoneNumber:
 
Adding a new PhoneNumber:
  
<source lang="java">
+
<pre>
 
PhoneNumber homePhoneNumber  = new ObjectFactory().createPhoneNumber();
 
PhoneNumber homePhoneNumber  = new ObjectFactory().createPhoneNumber();
 
homePhoneNumber.setType("home");
 
homePhoneNumber.setType("home");
 
homePhoneNumber.setValue("(613) 555-3333");
 
homePhoneNumber.setValue("(613) 555-3333");
 
customer.getContactInfo().getPhoneNumber().add(homePhoneNumber);
 
customer.getContactInfo().getPhoneNumber().add(homePhoneNumber);
</source>
+
</pre>
  
 
Note the use of the <tt>ObjectFactory</tt> class to create a new <tt>PhoneNumber</tt> object.  In addition to generating the model classes used in this example, the JAXB compiler also generates an <tt>ObjectFactory</tt> class that can be used to create any of the generated types.
 
Note the use of the <tt>ObjectFactory</tt> class to create a new <tt>PhoneNumber</tt> object.  In addition to generating the model classes used in this example, the JAXB compiler also generates an <tt>ObjectFactory</tt> class that can be used to create any of the generated types.
Line 107: Line 107:
 
Removing all "cell" PhoneNumbers:
 
Removing all "cell" PhoneNumbers:
  
<source lang="java">
+
<pre>
 
ArrayList phoneNumbersToRemove = new ArrayList();
 
ArrayList phoneNumbersToRemove = new ArrayList();
 
List phoneNumbers = customer.getContactInfo().getPhoneNumber();
 
List phoneNumbers = customer.getContactInfo().getPhoneNumber();
Line 118: Line 118:
 
}
 
}
 
phoneNumbers.removeAll(phoneNumbersToRemove);
 
phoneNumbers.removeAll(phoneNumbersToRemove);
</source>
+
</pre>
  
 
==Download==
 
==Download==
  
 
[http://wiki.eclipse.org/images/8/85/Org.eclipse.persistence.example.jaxb.zip org.eclipse.persistence.example.jaxb.zip]
 
[http://wiki.eclipse.org/images/8/85/Org.eclipse.persistence.example.jaxb.zip org.eclipse.persistence.example.jaxb.zip]

Revision as of 16:32, 19 September 2008

Overview

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

  • Generate JAXB-annotated Java classes from an XML Schema using the JAXB Compiler
  • Unmarshal an XML Document
  • Modify the XML data using the interfaces generated by the compiler
  • Marshal the modified data to XML

Setup

  1. Ensure that you have EclipseLink correctly installed and configured for your environment. Please see EclipseLink/Installing and Configuring EclipseLink for more information.
  2. Ensure that you have ANT correctly installed and configured.
  3. Unzip the Example ZIP file to a new directory.
  4. Edit the env.properties file in the root directory of the example and specify the path to your EclipseLink jlib directory:
...
# Edit eclipselink.jlib to point to your EclipseLink jlib directory
eclipselink.jlib=C:/EclipseLink-1.0/eclipselink/jlib
...
</pre>

You can compile and run the Example at any time by typing <tt>ant</tt> from the Example directory.  

==Running the JAXB Compiler==

The JAXB compiler can be run to generate JAXB-annotated Java classes from an XML Schema:

<pre>
<ECLIPSELINK_HOME>/eclipselink/bin/jaxb-compiler.sh <source-file.xsd> [-options]
<ECLIPSELINK_HOME>\eclipselink\bin\jaxb-compiler.cmd <source-file.xsd> [-options]

Options include:
   -d <dir>                     Specifies the output directory for generated files
   -p <pkg>                     Specifies the target package
   -classpath <arg>             Specifies where to find user class files
   -verbose                     Enable verbose output
   -quiet                       Suppress compiler output
   -version                     Display version information

For a complete list of compiler options:
   jaxb-compiler.sh -help

Example:
   jaxb-compiler.sh -d jaxb-compiler-output config/Customer.xsd 
</pre>

In this example, the JAXB Compiler is run from the "<tt>run.jaxb.compiler</tt>" task in the ANT build file.

==Creating a JAXBContext using jaxb.properties==

The standard way to specify which JAXB implementation should be used is through a file called <tt>jaxb.properties</tt>, which contains a single property, <tt>javax.xml.bind.context.factory</tt>. This file must be available on the classpath in the corresponding package (in this example, "<tt>org.example.customer_example</tt>").  To specify that the EclipseLink JAXB implementation should be used, your <tt>jaxb.properties</tt> file should have the following content:

<pre>
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
</pre>

In our example, we create the JAXBContext as follows:

<pre>
JAXBContext jaxbContext = JAXBContext.newInstance("org.example.customer_example");
</pre>

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

<pre>
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);
</pre>

==Unmarshalling the XML Document==

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

<pre>
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();
</pre>

==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:

<pre>
customer.getContactInfo().getShippingAddress().setZipCode("27601");
</pre>

Adding a new PhoneNumber:

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

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

Removing all "cell" PhoneNumbers:

<pre>
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);
</pre>

==Download==

[http://wiki.eclipse.org/images/8/85/Org.eclipse.persistence.example.jaxb.zip org.eclipse.persistence.example.jaxb.zip]