Data binding rules for Java beans from XML schema files

This section describes the rules that are used to generate beans from an XML schema. They are only applicable for the DOM Based XSD Beans Generator (which is deprecated).

Complex type

A complex type at the XML schema file level is mapped to a bean that subclasses from a ComplexType. Each subelement of the complex type is mapped as described in the Element section.

Element

An element results in the generation of set, get, and remove methods. The set method sets the element to the specified value. It will create the element if it does not already exist. The get method returns the element value. The remove method removes the element from its parent. The parameter for 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 get, set, and remove methods will look like this:
public void setPartNum(String partNum);
 public String getPartNum();
 public boolean removePartNum();
For the element address whose type is Address, the get, set, and remove methods will look like this:
public void setAddress(Address address)
public Address getAddress();
public boolean removeAddress();

The Data Type table later in this section 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 the anonymous complex type is generated as an inner class.

The inner class will contain the corresponding get, set, and remove methods for its contents. In the preceding example, an inner class Items.item will be generated. The get, set, and remove methods for item will look like this:

public void setItem(Items.item item);
 public Items.item getItem();
public boolean removeItem();

If the attribute maxOccurs is set to unbounded on an element, then one additional method is generated, getXXXCount().The get, set and remove methods will have an additional index parameter for specifying the particular element that we want to operate on. If we modify the preceding item element to add a maxOccursattribute as follows:

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

The following methods will be generated:

public void setItem(int index, Items.item item);
public Items.item getItem(int index);
public int getItemCount();
public boolean removeItem(int index);

Attribute

An attribute element results in the generation of a set, a get, and a remove method. The set method sets the attribute to the specified value. It will create the attribute if it not already exists. The get method returns the attribute value. The remove method removes the attribute from its parent element. 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 one of the following types of beans:

For example, the following purchaseOrder global element is an instance of the PurchaseOrder complex type. <element name="puchaseOrder" type="PurchaseOrderType"> A class purchaseOrder will be generated. The purchaseOrder class extends the generate bean PurchaseOrderType which extends ComplexType.

Simple type

A simple type at the XML schema file level is mapped to a bean that subclasses from SimpleType.

Data Types

Where possible, the XML schema built-in data types are mapped to a Java data type using the corresponding Java developer kit 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 beans. They are:

Related tasks
Generating Java beans from an XML schema

Feedback