Sun Java System Application Server 

Samples Index

JAXB 2.0 Unmarshal & Read Sample Application

This sample application demonstrates how to unmarshal an instance document into a Java content tree and access data contained within it.

This sample first complies the "purchase order" schema by using JAXB's schema compiler XJC, to generate Java classes that represent the schema:


<xjc schema="po.xsd" package="primer.po" destdir="gen-src">
  <produces dir="gen-src/primer.po" includes="**/*.java" />
</xjc>

Then the following code is used to read an XML file into a Java object:

// create an Unmarshaller
Unmarshaller u = jc.createUnmarshaller();

// unmarshal a po instance document into a tree of Java content
// objects composed of classes from the primer.po package.
JAXBElement<?> poElement = (JAXBElement<?>)u.unmarshal( new File( "po.xml" ) );
PurchaseOrderType po = (PurchaseOrderType)poElement.getValue();

Building and Running the Application

Following are the instructions for building and running this sample application.

  1. Setup your build environment and Configure the application server with which the build system has to work by following the common build instructions.
  2. app_dir is the sample application base directory: samples_install_dir/javaee5/webservices/simple-jaxb.
  3. Change directory to app_dir.
  4. Build and run the sample application using the target all
  5. app_dir> ant all

  6. It is possible to do the above separately by issuing separate commands as given below.
  7. app_dir> ant compile compiles and packages the application

    app_dir> ant run runs the program

    app_dir> ant javadoc to generate javadoc for the generated Java source code

  8. Use the target clean to remove the temporary directories like build and dist.

    app_dir> ant clean

Building, Deploying, and Running the Application in NetBeans IDE

Follow these instructions to build, deploy, and run this sample application using NetBeans IDE.

  1. Refer to common build instructions. for setting up NetBeans IDE and the application server with which the IDE will use.
  2. Make usre that you have defined the appropriate values in build.properties based on above common build instructions which is a pre-requisite for this sample.
  3. In NetBeans IDE, select File->OpenProject and select samples_install_dir/javaee5/webservices/simple-jaxb as the project.
  4. Right click on simple-jaxb and select Run Project which will build, deploy and run the project. Sample output is given below.
  5. Building index for all classes...
    run:
    Running the sample application...
    Ship the following items to: 
            Alice Smith
            123 Maple Street
            Cambridge, MA 12345
            US
    
            5 copies of "Nosferatu - Special Edition (1929)"
            3 copies of "The Mummy (1959)"
            3 copies of "Godzilla and Mothra: Battle for Earth/Godzilla vs. King Ghidora"
    BUILD SUCCESSFUL (total time: 6 seconds)
      

Troubleshooting

If you have problems when running the application, refer to troubleshooting document.

 


Copyright © 2006 Sun Microsystems, Inc. All rights reserved.