Schema validation with a DOM parser

In this section, we shall validate the example XML document, catalog.xml, against catalog.xsd with the DOMParser class. The procedure to validate with a DOMParser is the same as with a SAXParser, except that the parser class is different. First, import the oracle.xml.parser.schema and the oracle.xml.parser.v2 packages.

Creating a DOM parser

Create a DOMParser object and set validation mode to SCHEMA_VALIDATION, as shown in the following listing:

DOMParser domParser=new DOMParser(); domParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);

Create an XMLSchema object, which represents the DOM structure of an XML schema document, from the example schema document. To create an XMLSchema object, first create an XSDBuilder object. Next, create an InputStream object from catalog.xsd and subsequently create an InputSource object from the InputStream object. Create an XMLSchema object with the build(InputSource) method of the XSDBuilder class. The procedure to obtain an XMLSchema object is shown in the following listing:

XSDBuilder builder = new XSDBuilder();
InputStream inputStream=new FileInputStream(new File("catalog.xsd"));
InputSource inputSource=new InputSource(inputStream);
XMLSchema schema = builder.build(inputSource);

Set the XMLSchema object on the DOMParser object with the setXMLSchema(XMLSchema) method:

domParser.setXMLSchema(schema);

Setting the error handler

As in the SAXParser section, define an error handling class, CustomErrorHandler. Create a CustomErrorHandler object, and register the ErrorHandler object with the DOMParser using the setErrorHandler method.

CustomErrorHandler errorHandler = new CustomErrorHandler();
domParser.setErrorHandler(errorHandler);

We have used a SAX-based error handler for a DOM parser because most DOM parsers use a SAX parser internally.

Parsing the XML document

The DOMParser class extends the XMLParser class that provides the overloaded parse methods discussed in the previous section to parse an XML document. The preferred parse method is parse(InputSource) because the DOM parser uses an SAX parser internally and SAX parsers use an InputSource object for parsing. If another input type is specified, the input is converted to an InputSource by the SAX parser. Create an InputStream object from the example XML document, catalog.xml, and create an InputSource object from the InputStream object. Subsequently, parse the XML document using the parse(InputSource) method:

InputStream inputStream=new FileInputStream(new
File("catalog.xml"));
InputSource inputSource=new InputSource(inputStream);
domParser.parse(inputSource);

Running the Java application

The validation application DOMValidator.java is listed in the following listing with explanations:

  1. First, we declare the import statements for the classes that we need.
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import oracle.xml.parser.schema.*;
    import oracle.xml.parser.v2.*;
    import java.io.IOException;
    import java.io.InputStream;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.xml.sax.helpers.DefaultHandler;
    import org.xml.sax.InputSource;
    
  2. We define the Java class DOMValidator.
    public class DOMValidator {
    
  3. In the Java class we add a method validateXMLDocument.
    public void validateXMLDocument(InputSource input) {
    try {
    
  4. We create a DOMParser object and set the XML schema on the DOMParser.
    DOMParser domParser = new DOMParser();
    domParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);
    XMLSchema schema=getXMLSchema();
    domParser.setXMLSchema(schema);
    
  5. We create a CustomErrorHandler object and set the error handler on the DOMParser. We parse the XML document and output the validation errors, if any.
    CustomErrorHandler errorHandler = new CustomErrorHandler();
    domParser.setErrorHandler(errorHandler);
    domParser.parse(input);
    if (errorHandler.hasValidationError == true) {
    System.err.println("XML Document has
    Validation Error:" + errorHandler.saxParseException.getMessage());
    } else {
    System.out.println("XML Document validates
    with XML schema");
    }
    } catch (IOException e) {
    System.err.println("IOException " + e.getMessage());
    } catch (SAXException e) {
    System.err.println("SAXException " + e.getMessage());
    }
    }
    
  6. We define the getXMLSchema method to create an XMLSchema object.
    public XMLSchema getXMLSchema() {
    try {
    XSDBuilder builder = new XSDBuilder();
    InputStream inputStream =
    new FileInputStream(new File("catalog.xsd"));
    InputSource inputSource = new InputSource(inputStream);
    XMLSchema schema = builder.build(inputSource);
    return schema;
    } catch (XSDException e) {
    System.err.println("XSDException " + e.getMessage());
    } catch (FileNotFoundException e) {
    System.err.println("FileNotFoundException " +
    e.getMessage());
    }
    return null;
    }
    
  7. We add the main method in which we create an instance of the DOMValidator class and invoke the validateXMLDocument method.
    public static void main(String[] argv) {
    try {
    InputStream inputStream =
    new FileInputStream(new File("catalog.xml"));
    InputSource inputSource=new InputSource(inputStream);
    DOMValidator validator = new DOMValidator();
    validator.validateXMLDocument(inputSource);
    } catch (FileNotFoundException e) {
    System.err.println("FileNotFoundException " +
    e.getMessage());
    }
    }
    
  8. Finally, we define the CustomErrorHandler inner class.
    private class CustomErrorHandler extends DefaultHandler {
    protected boolean hasValidationError = false;
    protected SAXParseException saxParseException = null;
    public void error(SAXParseException exception)
    {
    hasValidationError = true;
    saxParseException = exception;
    }
    public void fatalError(SAXParseException exception)
    {
    hasValidationError = true;
    saxParseException = exception;
    }
    public void warning(SAXParseException exception)
    {
    }
    }
    }
    

To demonstrate error handling, add a title element to the journal element. A title element is not valid in a journal element.

Copy the DOMValidator.java application to the DOMValidator.java application in SchemaValidation project. To run the DOMValidator.java application, right-click on DOMValidator.java in Application Navigator, and select Run. A validation error gets outputted, indicating that the title element is not valid. If the title element in the journal element is removed and the DOMValidator.java application is re-run, a validation message gets outputted to indicate that the XML document is valid.

Running the Java application