The Input Source object is created with an Input Stream object created from the example XML schema document, As discussed before, we have used an Input Source object because most SAX implementations are Input Source based.The procedure to obtain an XMLSchema object is shown in the following listing: XSDBuilder builder = new XSDBuilder(); Input Stream input Stream=new File Input Stream(new File("catalog.xsd")); Input Source input Source=new Input Source(input Stream); XMLSchema schema = Source); As in the previous section, define an error handling class, Custom Error Handler that extends Default Handler class.

sax validating-85

If the parser is not able find a schema or if the XML document does not conform to the schema, an error is raised.

Next, create an XMLSchema object from the schema document with which an XML document is to be validated.

An XMLSchema object represents the DOM structure of an XML schema document and is created with an XSDBuilder class object.

Create an XSDBuilder object and invoke the build(Input Source) method of the XSDBuilder object to obtain an XMLSchema object.

The choice of validation method depends on the additional functionality required in the validation application.

SAXParser is recommended if SAX parsing event notification is required in addition to validation with a schema.DOMParser is recommended if the DOM tree structure of an XML document is required for random access and modification of the XML document.In this section we shall validate the example XML document with XML schema document catalog.xsd, with the SAXParser class. Create a SAXParser object and set the validation mode of the SAXParser object to SCHEMA_VALIDATION, as shown in the following listing: Validates the complete XML document with an XML schema if the parser is able to find a schema.Copy the application to in the Schema Validation project.To demonstrate error handling, add a title element to the journal element.To run the application, right-click on in Application Navigator, and select Run. The validation error indicates that the title element is not expected.