Java Reference
In-Depth Information
Begin by creating a
SchemaFactory
for a specific type of schema definition. A
SchemaFactory
knows how to parse a particular schema type and prepares it for
validation. Use the
SchemaFactory
instance to create a
Schema
object. The
Schema
object is an in-memory representation of the schema definition grammar. You
can use the
Schema
instance to retrieve a
Validator
instance that understands this
grammar. Finally, use the
validate()
method to check your XML. The method call
will generate several exceptions if anything goes wrong during the validation. Other-
wise, the
validate()
method returns quietly, and you can continue to use the XML
file.
Note
The XML Schema was the first to receive “Recommendation” status from the
World Wide Web consortium (W3C) in 2001. Competing schemas have since become
available. One competing schema is the Regular Language for XML Next Generation
(RELAX NG) schema. RELAX NG may be a simpler schema and its specification also
defines a non-XML, compact syntax. This recipe's example uses the XML schema.
Run the example code using the following command-line syntax, preferably with
the sample
.xml
file and validation files provided as
resources/patients.xml
and
patients.xsl
, respectively:
java org.java8recipes.chapter20.recipe20_4.ValidateXml
<xmlFile><validationFile>
20-5. Creating Java Bindings for an XML
Schema
Problem
You want to generate a set of Java classes (Java bindings) that represent the objects in
an XML schema.
Solution