Java Reference
In-Depth Information
The JDK provides a tool that can turn schema documents into representative Java class
files. Use the
<JDK_HOME>/bin/xjc
command-line tool to generate Java bindings
for your XML schemas. To create the Java classes for the
patients.xsd
file from
Recipe 20-3, you could issue the following command from within a console:
xjc -p org.java8recipes.chapter20.recipe20_5 patients.xsd
This command will process the
patients.xsd
file and create all the classes
needed to process an XML file that validates with this schema. For this example, the
patients.xsd
file looks like the following:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="patients">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="patient"
type="Patient"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Patient">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="diagnosis" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
</xs:schema>
Executed on the previous
xsd
file, the
xjc
command creates the following files in
the
org.java8recipes.chapter20.recipe20_5
package:
•
ObjectFactory.java
•
Patients.java
•
Patient.java