Document Information

Preface

Part I Introduction

1.  Overview

2.  Using the Tutorial Examples

Part II The Web Tier

3.  Getting Started with Web Applications

4.  Java Servlet Technology

5.  JavaServer Pages Technology

6.  JavaServer Pages Documents

7.  JavaServer Pages Standard Tag Library

8.  Custom Tags in JSP Pages

9.  Scripting in JSP Pages

10.  JavaServer Faces Technology

11.  Using JavaServer Faces Technology in JSP Pages

12.  Developing with JavaServer Faces Technology

13.  Creating Custom UI Components

14.  Configuring JavaServer Faces Applications

15.  Internationalizing and Localizing Web Applications

Part III Web Services

16.  Building Web Services with JAX-WS

17.  Binding between XML Schema and Java Classes

JAXB Architecture

Architectural Overview

The JAXB Binding Process

More about Unmarshalling

More about Marshalling

More about Validation

Representing XML Content

Java Representation of XML Schema

Binding XML Schemas

Simple Type Definitions

Default Data Type Bindings

Schema-to-Java Mapping

JAXBElement Object

Java-to-Schema Mapping

Customizing Generated Classes and Java Program Elements

Schema-to-Java

Java-to-Schema

JAXB Examples

JAXB Compiler Options

JAXB Schema Generator Option

About the Schema-to-Java Bindings

Schema-Derived JAXB Classes

Comment Class

Items Class

ObjectFactory Class

PurchaseOrder Class

PurchaseOrderType Class

USAddress Class

Customizing JAXB Bindings

Why Customize?

Customization Overview

Inline and External Customizations

Scope, Inheritance, and Precedence

Customization Syntax

Customization Namespace Prefix

Customize Inline Example

Building and Running the Customize Inline Example Using NetBeans IDE

Building and Running the Customize Inline Example Using Ant

Customized Schema

Global Binding Declarations

Schema Binding Declarations

Class Binding Declarations

Property Binding Declarations

MyDatatypeConverter Class

Datatype Converter Example

Building and Running the Datatype Converter Example Using NetBeans IDE

Building and Running the Datatype Converter Example Using Ant

Binding Declaration Files

JAXB Version, Namespace, and Schema Attributes

Global and Schema Binding Declarations

Class Declarations

External Customize Example

Building and Running the External Customize Example Using NetBeans IDE

Building and Running the External Customize Example Using Ant

Java-to-Schema Examples

Create Marshal Example

Building and Running the Create Marshal Example Using NetBeans IDE

Building and Running the Create Marshal Example Using Ant

XmlAccessorOrder Example

Using the @XmlAccessorOrder Annotation to Define Schema Element Ordering

Using the @XmlType Annotation to Define Schema Element Ordering

Schema Content Ordering in the Example

Building and Running the XmlAccessorOrder Example Using NetBeans IDE

Building and Running the XmlAccessorOrder Example Using Ant

XmlAdapter Field Example

Building and Running the XmlAdapter Field Example Using NetBeans IDE

Building and Running the XmlAdapter Field Example Using Ant

XmlAttribute Field Example

Building and Running the XmlAttribute Field Example Using NetBeans IDE

Building and Running the XmlAttribute Field Example Using Ant

XmlRootElement Example

Building and Running the XmlRootElement Example Using NetBeans IDE

Building and Running the XmlRootElement Example Using Ant

XmlSchemaType Class Example

Building and Running the XmlSchemaType Class Example Using NetBeans IDE

Building and Running the XmlSchemaType Class Example Using Ant

XmlType Example

Building and Running the XmlType Example Using NetBeans IDE

Building and Running the XmlType Example Using Ant

Further Information about JAXB

18.  Streaming API for XML

19.  SOAP with Attachments API for Java

Part IV Enterprise Beans

20.  Enterprise Beans

21.  Getting Started with Enterprise Beans

22.  Session Bean Examples

23.  A Message-Driven Bean Example

Part V Persistence

24.  Introduction to the Java Persistence API

25.  Persistence in the Web Tier

26.  Persistence in the EJB Tier

27.  The Java Persistence Query Language

Part VI Services

28.  Introduction to Security in the Java EE Platform

29.  Securing Java EE Applications

30.  Securing Web Applications

31.  The Java Message Service API

32.  Java EE Examples Using the JMS API

33.  Transactions

34.  Resource Connections

35.  Connector Architecture

Part VII Case Studies

36.  The Coffee Break Application

37.  The Duke's Bank Application

Part VIII Appendixes

A.  Java Encoding Schemes

B.  About the Authors

Index

 

Basic JAXB Examples

This section describes the Basic examples (Modify Marshal, Unmarshal Validate) that demonstrate how to:

  • Unmarshal an XML document into a Java content tree and access the data contained within it

  • Modify a Java content tree

  • Use the ObjectFactory class to create a Java content tree from scratch and then marshal it to XML data

  • Perform validation during unmarshalling

  • Validate a Java content tree at runtime

Modify Marshal Example

The Modify Marshal example demonstrates how to modify a Java content tree.

  1. The tut-install/javaeetutorial5/examples/jaxb/modify-marshal/src/modifymarshal/Main.java class declares imports for three standard Java classes plus four JAXB binding framework classes and primer.po package:

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.math.BigDecimal;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    import primer.po.*;
  2. A JAXBContext instance is created for handling classes generated in primer.po.

    JAXBContext jc = JAXBContext.newInstance( "primer.po" );
  3. An Unmarshaller instance is created, and po.xml is unmarshalled.

    Unmarshaller u = jc.createUnmarshaller();
    PurchaseOrder po = (PurchaseOrder)u.unmarshal( new FileInputStream( "po.xml" ) );
  4. set methods are used to modify information in the address branch of the content tree.

    USAddress address = po.getBillTo();
    address.setName( "John Bob" );
    address.setStreet( "242 Main Street" );
    address.setCity( "Beverly Hills" );
    address.setState( "CA" );
    address.setZip( new BigDecimal( "90210" ) );
  5. A Marshaller instance is created, and the updated XML content is marshalled to system.out. The setProperty API is used to specify output encoding; in this case formatted (human readable) XML format.

    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal( po, System.out );
Building and Running the Modify Marshal Example Using NetBeans IDE

Follow these instructions to build and run the Modify Marshal example on your Application Server instance using NetBeans IDE.

  1. In NetBeans IDE, select File→Open Project.

  2. In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/jaxb/.

  3. Select the modify-marshal folder.

  4. Select the Open as Main Project check box.

  5. Click Open Project.

  6. Right-click the modify-marshal project and select Run.

Building and Running the Modify Marshal Example Using Ant

To compile and run the Modify Marshal example using Ant, in a terminal window, go to the tut-install/javaeetutorial5/examples/jaxb/modify-marshal/ directory and type the following:

ant runapp

Unmarshal Validate Example

The Unmarshal Validate example demonstrates how to enable validation during unmarshalling. Note that JAXB provides functions for validation during unmarshalling but not during marshalling. Validation is explained in more detail in More about Validation.

  1. The tut-install/javaeetutorial5/examples/jaxb/unmarshal-validate/src/unmarshalvalidate/Main.java class declares imports for three standard Java classes plus seven JAXB binding framework classes and the primer.po package:

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.math.BigDecimal;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.UnmarshalException;
    import javax.xml.bind.Unmarshaller;
    import javax.xml.bind.ValidationEvent;
    import javax.xml.bind.util.ValidationEventCollector;
    import primer.po.*;
  2. A JAXBContext instance is created for handling classes generated in primer.po.

    JAXBContext jc = JAXBContext.newInstance( "primer.po" );
  3. An Unmarshaller instance is created.

    Unmarshaller u = jc.createUnmarshaller();
  4. The default JAXB Unmarshaller ValidationEventHandler is enabled to send to validation warnings and errors to system.out. The default configuration causes the unmarshal operation to fail upon encountering the first validation error.

    u.setValidating( true );
  5. An attempt is made to unmarshal po.xml into a Java content tree. For the purposes of this example, the po.xml contains a deliberate error.

    PurchaseOrder po = (PurchaseOrder)u.unmarshal( new FileInputStream("po.xml"));
  6. The default validation event handler processes a validation error, generates output to system.out, and then an exception is thrown.

    } catch( UnmarshalException ue ) {
        System.out.println( "Caught UnmarshalException" );
    } catch( JAXBException je ) { 
        je.printStackTrace();
    } catch( IOException ioe ) {
        ioe.printStackTrace();
    }
Building and Running the Unmarshal Validate Example Using NetBeans IDE

Follow these instructions to build and run the Unmarshal Validate example on your Application Server instance using NetBeans IDE.

  1. In NetBeans IDE, select File→Open Project.

  2. In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/jaxb/.

  3. Select the unmarshal-validate folder.

  4. Select the Open as Main Project check box.

  5. Click Open Project.

  6. Right-click the unmarshal-validate project and select Run.

Building and Running the Unmarshal Validate Example Using Ant

To compile and run the Unmarshal Validate example using Ant, in a terminal window, go to the tut-install/javaeetutorial5/examples/jaxb/unmarshal-validate/ directory and type the following:

ant runapp