Oracle TopLink JAXB 1.0

How-To Document

January 2006

After reading this How-To document, you will be able to:

Software Requirements

Conventions

The following conventions are used in this document:

Introduction

Oracle TopLink now includes a JAXB 1.0 implementation as part of its object-to-XML support. This How-To document will show you how to use TopLink to create a JAXB application. The TopLink JAXB compiler will be used to produce the content model. Using the content model, you will unmarshal an XML document, manipulate the data, and then marshal the data back to XML.

To further customize the content model, you will use the TopLink Workbench.

Using TopLink JAXB

In this section of this How-To document, you will use the Customer.xsd XML schema to generate a JAXB object model and all the necessary mapping metadata that your application will need. You will then use the generated artifacts to manipulate an XML document (Customer-data.xml).

Step 1: Directory Setup

Create the <DEMO_HOME> directory and place the Customer.xsd at the root of the directory.

Step 2: Create TopLink Workbench Project

In this How-To, you use the TopLink Workbench to run the TopLink JAXB compiler to generate a TopLink Workbench project, model classes, and mappings automatically.

  1. Execute the workbench.cmd (Windows) or the workbench.sh (UNIX) file to start the TopLink Workbench, located in the <ORACLE_HOME>/bin directory.

  2. From the File menu, select New > Project From XML Schema (JAXB).

    The Create TopLink Workbench Project using JAXB dialog appears.

    Create TopLink Workbench Project using JAXB dialog

    Description of this figure follows
    Description of "Create TopLink Workbench Project using JAXB dialog"

  3. Enter the following into the Create TopLink Workbench Project using JAXB dialog:

    • Schema File: <DEMO_HOME>/Customer.xsd

    • Optional Customization File: This is a JAXB-specific file used to customize how the generation will occur. For this How-To, leave this field blank.

    • Output Directory: <DEMO_HOME>/jaxb-output

    • Output Source Directory: ./source

    • Output Workbench Project Directory: ./mw

    • Package Name for Generated Interfaces: examples.jaxb.model

    • Package Name for Generated Implementation Classes: examples.jaxb.model

  4. Click OK.

    The TopLink Workbench generates all of the required source files. Upon completion, the new TopLink Workbench project, model class descriptors, and mappings appear.

    Review the source tree to view the generated classes and the TopLink Workbench project file. You will need the classes generated, the jaxb.properties file, and the TopLink mapping files (sessions.xml and Customer.xml) to use this in an application. The remainder of the files are development time artifacts only.

Step 3: Customize the TopLink Metadata (Optional)

Together with the standard artifacts, the TopLink JAXB compiler produces a TopLink Workbench project. At this point, any of the TopLink object-to-XML features can be applied to customize the object model. For the purposes of this How-To document, you will add an Object Type Converter to the _Gender attribute of the PersonalInfoTypeImpl class.

Create TopLink Workbench Project from JAXB dialog

Description of this figure follows
Description of "Create TopLink Workbench Project from JAXB dialog"

Step 4: Write a Simple Test Application

To write a simple test application, you will need to set the required classpath as follows:

The content model will now be used to unmarshal an XML document from a file (Customer-data.xml), manipulate the data, and then marshal the data back to XML.

The first step is to create a <DEMO_HOME>/classes folder and compile the generated source files there. Copy the TopLink mapping files (sessions.xml and Customer.xml) to the classes folder.

The next step is to create a JAXBContext. The JAXBContext provides the client's entry point to the JAXB API. The input to the newInstance method corresponds to the value you entered for Package Name for Generated Implementation Classes when you created the TopLink Workbench project.

javax.xml.bind.JAXBContext jaxbContext = JAXBContext.newInstance("examples.jaxb.model");

Next, you must unmarshal the XML data to object form. Use the JAXBContext to create an instance of the Unmarshaller class. The Unmarshaller class can accept many types of XML input. For the purpose of this How-To document, you will use the Customer-data.xml file.

java.io.File file = new java.io.File("Customer-data.xml");
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) unmarshaller.unmarshal(file);

Now that you have the data in object form, you can manipulate it. The sample code demonstrates how to add a PhoneNumber. Notice how only a few lines of code are required compared to what would be required if this was done using DOM programming.

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

Finally you will marshal the object data back to XML form. To do this, you can use the JAXBContext to create an instance of the Marshaller class. The Marshaller class can generate many types of XML output. For the purpose of this How-To document, you will use System.out.

javax.xml.bind.Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(customer, System.out);

Summary

TopLink provides a powerful and flexible object-XML capability that complements the existing object-relational mapping functionality with which many are already familiar. In this release, TopLink goes beyond JAXB 1.0 to provide developers with a rich object-level programmatic environment to work with XML data.


Oracle TopLink, 10g Release 3 (10.1.3)

The Programs (which include both the software and documentation) contain proprietary information; they are provided under a license agreement containing restrictions on use and disclosure and are also protected by copyright, patent, and other intellectual and industrial property laws. Reverse engineering, disassembly, or decompilation of the Programs, except to the extent required to obtain interoperability with other independently created software or as specified by law, is prohibited.

The information contained in this document is subject to change without notice. If you find any problems in the documentation, please report them to us in writing. This document is not warranted to be error-free. Except as may be expressly permitted in your license agreement for these Programs, no part of these Programs may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose.

If the Programs are delivered to the United States Government or anyone licensing or using the Programs on behalf of the United States Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data delivered to U.S. Government customers are "commercial computer software" or "commercial technical data" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the Programs, including documentation and technical data, shall be subject to the licensing restrictions set forth in the applicable Oracle license agreement, and, to the extent applicable, the additional rights set forth in FAR 52.227-19, Commercial Computer Software—Restricted Rights (June 1987). Oracle Corporation, 500 Oracle Parkway, Redwood City, CA 94065

The Programs are not intended for use in any nuclear, aviation, mass transit, medical, or other inherently dangerous applications. It shall be the licensee's responsibility to take all appropriate fail-safe, backup, redundancy and other measures to ensure the safe use of such applications if the Programs are used for such purposes, and we disclaim liability for any damages caused by such use of the Programs.

Oracle, JD Edwards and PeopleSoft are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

The Programs may provide links to Web sites and access to content, products, and services from third parties. Oracle is not responsible for the availability of, or any content provided on, third-party Web sites. You bear all risks associated with the use of such content. If you choose to purchase any products or services from a third party, the relationship is directly between you and the third party. Oracle is not responsible for: (a) the quality of third-party products or services; or (b) fulfilling any of the terms of the agreement with the third party, including delivery of products or services and warranty obligations related to purchased products or services. Oracle is not responsible for any loss or damage of any sort that you may incur from dealing with any third party.