In this article
, Elliotte Rusty Harold shows how to check documents for conformance to schemas with the Java XML validation API.
The API by which programs request validation has varied with the schema language and parser - until now. The javax.xml.validation package provides a schema-language-independent interface to validation services.
The javax.xml.validation API uses three classes to validate documents: SchemaFactory, Schema and Validator. It also makes extensive use of the javax.xml.transform.Source interface from TrAX to represent the XML documents. In brief, a SchemaFactory reads the schema document (often an XMl file) from which it creates a Schema object. The Schema object creates a Validator object. Finally, the Validator object validates an XML document represented as a Source.
Harold goes on to explain how abstract factories enable one API to support many different schema languages and object models, and how error handlers can help determine more detailed information beyond the default SAXException response from a schema. And where the W3C XML Schema Language is heavily based on types, Harold also shows how the Java Validation API can identify and report these types.
Types are identified by an org.w3c.dom.TypeInfo object. This simple interface...tells you the local name and namespace URI of a type. You can also tell whether and how a type is derived from another type. Beyond that, understanding the type is up to your program. The Java language doesn't tell you what it means or convert the data to a Java type such as double or java.util.Date.
To get TypeInfo objects, you ask the Schema object for a ValidatorHandler rather than a Validator. ValidatorHandler implements SAX's ContentHandler interface. Then, you install this handler in a SAX parser.
You also install your own ContentHandler in the ValidatorHandler (not the parser); the ValidatorHandler will forward the augmented events on to your ContentHandler.
The ValidatorHandler makes available a TypeInfoProvider that your ContentHandler can call at any time to find out the type of the current element or one of its attributes. It can also tell you whether an attribute is an ID, and whether the attribute was explicitly specified in the document or defaulted in from the schema.
javax.xml.validation offers an API that can handle the various useful XML schema languages that are out there. Are you using this package for validation?