Generating Java classes for XML Schema

To allow developers to quickly build an XML application, the DTD Editor and the XML Schema Editor supports the generation of Java beans from a DTD or an XML Schema. Using these Java beans, you can quickly create an instance document or load an instance document that conforms to the DTD or the XML Schema without coding directly to the DOM APIs.


Create Java Beans Wizard

Use the Create Java Beans wizard to generate the Java Beans when you are within the DTD Editor or the XML Schema Editor. A command line utility is also provided. You will need to specify the following parameters in the wizard: When you click Finish, the following files will be generated:
  1. A Java bean for each construct (e.g. a complex type, a global element) at the XML Schema file level. The name of the construct will be used as the name of the Java bean.
  2. A Factory class for class construction. The name of the root element will be used to construct this class. For example if the root element is PurchaseOrder, the factory class will be called PurchaseOrderFactory.
  3. Optionally, a Sample main program.
Step-by-step instructions on how to generate, compile, and run an application using the generated Java beans, both inside and outside the Workbench is also provided.


Binding Rules

This section describes the rules that we use to generate the Java beans from a schema. A DTD is first mapped to an XML Schema before the Java beans are generated. Therefore the terminology used below will be based on XML Schema terminology.

Complex Type

A complex type at the XML Schema file level is mapped to a Java bean that subclasses from IBMXMLElement. Each subelement of the complex type is mapped as follows:

Element

An element results in the generation of a pair of get/set methods to get and set the element. The parameter to the set method and the return result of the get method will be based on the element type. Consider the following complex type Items:

<complexType name="Items">
        <sequence>
                <element name="PartNum" type="string">
                <element name="address" type="Address">
                <element name="item">
                        <complexType>
                                <sequence>
                                        <element name="productName" type="String">
                                </sequence>
                        </complexType>
                </element>
                <element name="price">
                        <simpleType base="float">
                                <restriction>
                                            <maxExclusive="1000"/>
                                </restriction>
                        </simpleType>
                </element>
        </sequence>
</complexType>

For the element PartNum whose type is a string, the getter and setter method will look like this:
    public void setPartNum(String partNum);
    public String getPartNum();

For the element address whose type is Address, the getter and setter method will look like this:
    public void setAddress(Address address)
    public Address getAddress();

The Data Type table below shows the mapping of the built-in XML Schema data types to Java data types.

If the type of an element is an anonymous complex type, then we generate the anonymous complex type as an inner class. The inner class will contain the corresponding getters and setters for its contents. In the above example, an inner class Items.item will be generated. The getter and setter method for item will look like this:
    public void setItem(Items.item item);
    public Items.item getItem();

If the type of an element is an anonymous simple type, then the getter and setter method will be based on the base type of the simple type. For example, the getter and setter for price will look like this:
    public void setPrice(float price);
    public float getPrice();

If the attribute maxOccurs is set to unbounded on an element, then two methods are generated, getXXXCount() and getXXX(int index), to get the total number of elements and a particular element. Consider the following element declaration:

<element name="item" minOccurs="1" maxOccurs="unbounded">

The following methods will be generated:
    public Item getItem(int index);
    public int getItemCount();

Element Reference

An element reference results in the generation of a get method to get the referenced element. The data type mapping rule is the same as above.

Attribute

An attribute results in the generation of a pair of get/set methods to get and set the value of the attribute. The parameter to the set method and the return result of the get method will be based on the attribute type.

Global Element

A global element at the XML Schema file level is mapped to a Java bean that subclasses from IBMXMLElement. There are two general styles for a global element: In the first style, a client program can invoke the getElementValue() method on the purchaseOrder object to get the PurchaseOrderType object.

In the second style, the Java bean that is generated for PurchaseOrder will be very similar to the Java bean that will be generated for a complex type.

Simple Type

A simple type at the XML Schema file level is mapped to a Java bean that subclasses from IBMXMLAttribute.


Data Types

Where possible, the XML Schema builtin data types are mapped to a Java data type using the corresponding JDK class. The following table summarizes the data mapping. Any XML schema data types that are not listed are mapped as String.
 
XML Schema Type  Java data type
string String
integer, int
nonPositiveInteger, nonNegativeInteger
positiveInteger, negativeInteger, 
unsignedLong, unsignedInt,
int
boolean boolean
float float
double double
long long
short, unsignedShort short
byte, unsignedByte byte
date Date


Framework

The package com.ibm.etools.xmlschema.beans contains a number of framework classes that are used by the generated Java Beans. They are: The source code for the framework classes is included and they are located in plugins\xmlschemamodel\jars\xsdbeans.


Command Line Utility

A command line utility is provided to invoke the generation of the Java beans from outside the workbench..You can invoke the xsd2Java.bat file to generate the XML Schema and the Java classes from a command line. The xsd2Java.bat file is located in plugins\xmlschemamodel.

The following lists the command line switches:

        dtd           A DTD file name
        xsd           A XSD file name
        package   The package name for the Java beans (default = empty)
        root          The root element for your XML DOM
        sample     Generates a sample (default = false)
        dir            The output directory

Either the DTD filename or the XSD filename must be specified. If a DTD file name is specified as input, the corresponding XSL file will also be generated.

An example of invoking xsd2Java is as follows:

    xsd2Java -xsd PurchaseOrder.xsd -package demo -root PurchaseOrderType -sample -dir e:\project\demo