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.
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.
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 |
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