Java Reference
In-Depth Information
<?xml version="1.0"?>
<xs:schema targetNamespace="http://xml.product"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns:myco="http://xml.product">
<xs:element name="product" type="myco:Product"/>
<xs:complexType name="Product">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="description" type="xs:string"/>
<xs:element name="price" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Run the schema compiler tool on the command line as follows:
xjc Product.xsd
This command generates the source code for Java classes that correspond to the types
defined in the
.xsd
file. The schema compiler tool generates a Java class for each
com-
plexType
defined in the
.xsd
file. The fields of each generated Java class are the same
as the elements inside the corresponding
complexType
, and the class contains getter
and setter methods for these fields.
In this case the schema compiler tool generates the classes
product.xml.Product
and
product.xml.ObjectFactory
. The
Product
class contains JAXB annota-
tions, and its fields correspond to those in the
.xsd
definition:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Product", propOrder = {
"id",
"name",
"description",
"price"
})
public class Product {
protected int id;
@XmlElement(required = true)