Validating with the JAXP 1.3 Validation API
The validation API in J2SE 5.0 is included in the javax.xml.validation package. To validate with Validator class, import the javax.xml.validation package:
import javax.xml.validation.*;
To use this class with an XML Schema, you'll need a schema object representation of the schema. Create a schema object from the SchemaFactory class, which is a schema compiler you can obtain by using the static method newInstance(). Here's what the object should look like:
SchemaFactory factory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema=factory.newSchema(new File("c:/schemas/catalog.xsd"));
The only argument to the newInstance() method is a Schema language constant whose value is XMLConstants.W3C_XML_SCHEMA_NS_URI. This is the same as http://www.w3.org/2001/XMLSchema".Validator class validates an XML document with respect to an XML schema.
To create a Validator object from a Schema object:
Validator validator=schema.newValidator();
To report validation errors, define an ErrorHandler class for the Validator. This ErrorHandler class must extend the DefaultHandler, which implements the ErrorHandler interface:
private class ErrorHandlerImpl extends DefaultHandler
{
public boolean validationError = false;
public SAXParseException saxParseException=null;
public void error(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException=exception;
}
public void fatalError(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException=exception;
}
public void warning(SAXParseException exception) throws SAXException
{
}
}
Set an instance of an ErrorHandler class on the validator with the setErrorHandler() method:
ErrorHandlerImpl errorHandler=new ErrorHandlerImpl();
validator.setErrorHandler(errorHandler);
If a you generate a validation error, it's registered with the ErrorHandler. To validate an XML document, create a StreamSource from the XML document, invoke the validate() method on the validator, and pass it the stream source for the document:
StreamSource streamSource=new StreamSource(xmlDocument);
validator.validate(streamSource);
Listing 4 shows how to validate using the J2SE 5.0 validation API.
Run the XMLSchemaValidator.java application in your command line or your Java IDE. Here's what the output should be:
XML document is Valid
Now, add an error like you did in the JDOM section. This also generates a validation error.
There are benefits and drawbacks to each of these methods. With the JDOM API, validation is an integral part of XML parsing. But the JAXP 1.3 API decouples validation from the XML parsing process, thus increasing the performance of schema validation. The amount of performance gain you'll see is directly proportionate to the ratio size of XML schema/size of XML document. Another advantage to the JAXP 1.3 API is that you can use any schema language, whereas with JDOM, you must use the W3C XML Schema.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|