Generating and compiling the Java Beans for the XML Schema
This section provides the step-by-step instructions on how you can generate
and compile Java Beans for an XML Schema. The first section describes the
steps from within the WebSphere Studio Workbench. The second section describes
the steps using a command line.
In both sections, we will use the plugins\xsdeditor\samples\PurchaseOrder\PurchaseOrder.xsd
as the example.
Using the WebSphere Studio Workbench
Note: In this alpha, the Workbench only supports Java code compilation.
You cannot execute a main program using the Workbench Java IDE. Therefore,
to complete an application, you will need to use a command line interface.
However, you might still want to follow through the steps here to learn
about how you can use the Workbench Java IDE.
Steps:
-
Create a new solution say B2B.
-
Create a new Web project called MyProject.
-
Rename the servlets directory under MyProject to bin.
-
Select File->Import and import the PurchaseOrder.xsd file into the
folder MyProject/web.
-
Select File->New->Other->Java->Java Package to create new Java package
called PurchaseOrder under the folder B2B/MyProject/web.
-
Open the XML Schema Editor on the PurchaseOrder.xsd.
-
Select XSDEditor->Generate Java to generate the Java classes. The
Package Name is defaulted to PurchaseOrder. Accept the defaults as shown
below and say Finish. You will notice a number of Java classes being generated
into the folder B2B/MyProject/web/PurchaseOrder.
-
Now you need to set up your build class path. Right mouse button 2 on MyProject
and choose the Properties action. This will bring up the following page
for defining the Java Build Path:
Use the Add Folder button to add the classpath /B2B/MyProject/web.
Remove other paths.
Use the Add External Jar button, add in 2 external jars: the
plugins/b2bxmlrt/xerces.jar
for the XML parser, and the plugins/xmlschemamodel/jars/xdbeans.jar
for the framework classes.
Also, specify your Build output folder to /B2B/MyProject/bin.
Your finished configuration should look like the following. Please
note that the order of the entry is very important.
-
If you have successfully followed all these steps, when you say Ok to apply
the build class path, the incremental Java compiler will be invoked automatically,
and the .class files for the generated beans will be created under the
/B2B/MyProject/bin
directory. Your Workbench will look something like this if you switch to
the Java Perspective. You can now review the generated code using the Java
IDE.
Using a command line
To use a command line utility, the most important thing is to set up your
classpath so that it includes all the necessary .jar files. A number of
.bat file is created for your convenience:
Generating Java Beans
The plugins\xmlschemamodel\xsd2Java.bat is provided for your convenience.
Take a look at this file. It uses another file under plugins\xmlschemamodel\runtime\setjavatest.bat
to set up the classpath.
To make sure all the jars that are needed for generating the Java Beans
are included, you need to set the environment variable VABASE
to point to where you have installed the Workbench. For example:
set VABASE=f:\vabase\itp
Once you have this setup, you can run xsd2Java.bat to generate
the Java Beans. For example, type:
xsd2Java -xsd PurchaseOrder.xsd -root PurchaseOrderType -dir f:\project
to generate the Java Beans into f:\project directory
Compiling and running the generated code
Once you have generated your Java Beans, you might want to write a client
program to test out your Java Beans. An example of a client program is
provided in plugins\xsdeditor\samples\PurchaseOrder\PurchaseOrderSample.java
There is also a setpath.bat file in the same directory to help
you set up your classpath. To compile and run the Java Beans, you need
to include the xerces.jar and the xsdbeans.jar in your classpath.