Sun Java System Application Server |
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();
Following are the instructions for building and running
this sample application.
app_dir
is the sample application base
directory: samples_install_dir/javaee5/webservices/simple-jaxb
.
Change directory to app_dir.
app_dir>
ant
all
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
app_dir> ant
clean
Follow these instructions to build, deploy, and run this sample application using NetBeans IDE.
samples_install_dir/javaee5/webservices/simple-jaxb
as the project.simple-jaxb
and select Run Project
which will build, deploy and run the project. Sample output is given below. 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)
If you have problems when running the application, refer to troubleshooting document.
Copyright © 2006 Sun Microsystems, Inc. All rights reserved.