Java sax schema validation

https:‮www//‬.theitroad.com

The Simple API for XML (SAX) in Java provides a mechanism for validating XML documents against an XML schema using the SAX event-driven model. Here's an example of how to use SAX for schema validation in Java:

Suppose you have an XML document example.xml and an XML schema example.xsd. Here's the XML document:

<?xml version="1.0"?>
<catalog>
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>44.95</price>
    <publish_date>2000-10-01</publish_date>
    <description>An in-depth look at creating applications 
      with XML.</description>
  </book>
</catalog>

And here's the XML schema:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="catalog">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="author" type="xs:string"/>
              <xs:element name="title" type="xs:string"/>
              <xs:element name="genre" type="xs:string"/>
              <xs:element name="price" type="xs:decimal"/>
              <xs:element name="publish_date" type="xs:date"/>
              <xs:element name="description" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:string"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Here's an example of how to validate the XML document against the XML schema using SAX:

import javax.xml.XMLConstants;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
import java.io.IOException;

public class MyErrorHandler extends DefaultHandler {
    public void warning(SAXParseException e) throws SAXException {
        System.out.println("Warning: " + e.getMessage());
    }

    public void error(SAXParseException e) throws SAXException {
        System.out.println("Error: " + e.getMessage());
    }

    public void fatalError(SAXParseException e) throws SAXException {
        System.out.println("Fatal error: " + e.getMessage());
        throw e;
    }
}

public class MySaxSchemaValidator {
    public static void main(String[] args) {
        try {
            File xmlFile = new File("example.xml");
            File xsdFile = new File("example.xsd");

            // create a schema factory
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            // create a schema object
            Schema schema = schemaFactory.newSchema(xsdFile);

            // create a SAX parser factory
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();

            // set the schema on the SAX parser factory
            saxParserFactory.setSchema(schema);

            // create a SAX parser
            SAXParser saxParser = saxParserFactory.newSAXParser();

            // set the error handler
            saxParser.getXMLReader().setErrorHandler(new MyErrorHandler());