A Validator object validates an XML document against the Schema from which the Validator was created. The validate( ) method performs validation. Specify the document to be validated with a DOMSource or SAXSource object (from the javax.xml.transform.dom or javax.xml.transform.sax packages). The validate( ) method accepts any javax.xml.transform.Source object as an argument, but SAXSource and DOMSource are the only two supported implementations.

The document validation process can also be used to augment the source document by adding the default values of unspecified attributes. If you want to capture this augmented form of the document, pass a Result object to the two-argument version of validate( ). If the source is a SAXSource, the result must be a SAXResult, and if the source is a DOMSource, the result must be a DOMResult object.

If the document is valid, the validate( ) method returns normally. If the document is not valid, validate( ) throws an org.xml.sax.SAXException. You can alter this somewhat by passing a custom org.xml.sax.ErrorHandler to setErrorHandler( ). Validation exceptions are first passed to the error handler methods, which may throw the exception or handle them in some other way, such as printing a message. If the error handler does not throw an exception, the validate( ) method attempts to continue validation. The default error handler ignores exceptions passed to its warn( ) method but throws exceptions passed to its error( ) and ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.