# Click "Browse" to select a root directory, and point to the folder containing this example. After selecting the directory, you should see the project name in the "Projects" list. Click "Finish".

+

−

+

−

This project is configured to use a classpath variable, <tt>ECLIPSELINK_JLIB</tt>, to point to the required JAR files. After the project is imported, you should define a variable called <tt>ECLIPSELINK_JLIB</tt> to point to your EclipseLink <tt>jlib</tt> directory.

+

−

+

−

==Running the JAXB Compiler==

+

−

+

−

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

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:

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>

+

−

+

−

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

Then use an instance of this class with JAXBContext to capture the generated XML Schema.

+

−

+

−

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

+

−

+

−

SchemaOutputResolver sor = new MySchemaOutputResolver();

+

−

jaxbContext.generateSchema(sor);

+

−

</pre>

+

−

+

−

==Download==

+

−

+

−

Download the "Examples Zip" from the EclipseLink [http://www.eclipse.org/eclipselink/downloads/ Downloads] page. Code for this example will be found in the <tt>org.eclipse.persistence.example.jaxb.zip</tt> file.