# javax.xml.validation

***

**1. Validating an XML Document with a Schema**

```java
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;

public class ValidateXMLSchema {

    public static void main(String[] args) {
        try {
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new File("schema.xsd"));
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI);
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schema);
            parser.parse("document.xml", new DefaultHandler());
            System.out.println("Document is valid");
        } catch (SAXException | ParserConfigurationException | IOException e) {
            e.printStackTrace();
        }
    }
}
```

**2. Validating an XML Document with a DTD**

```java
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;

public class ValidateXMLDTD {

    public static void main(String[] args) {
        try {
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.XML_DTD_NS_URI);
            Schema schema = factory.newSchema(new File("dtd.dtd"));
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            parser.setProperty("http://apache.org/xml/properties/internal/dtd", true);
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schema);
            parser.parse("document.xml", new DefaultHandler());
            System.out.println("Document is valid");
        } catch (SAXException | ParserConfigurationException | IOException e) {
            e.printStackTrace();
        }
    }
}
```

**3. Validating an XML Document with a Relax NG Schema**

```java
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;

public class ValidateXMLRelaxNG {

    public static void main(String[] args) {
        try {
            SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema/relax-ng");
            Schema schema = factory.newSchema(new File("schema.rng"));
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema/relax-ng");
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schema);
            parser.parse("document.xml", new DefaultHandler());
            System.out.println("Document is valid");
        } catch (SAXException | ParserConfigurationException | IOException e) {
            e.printStackTrace();
        }
    }
}
```

**4. Validating an XML Document with a Schematron Schema**

```java
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;

public class ValidateXMLSchematron {

    public static void main(String[] args) {
        try {
            SchemaFactory factory = SchemaFactory.newInstance("http://www.schematron.com/schemas/schematron");
            Schema schema = factory.newSchema(new File("schema.sch"));
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.schematron.com/schemas/schematron");
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schema);
            parser.parse("document.xml", new DefaultHandler());
            System.out.println("Document is valid");
        } catch (SAXException | ParserConfigurationException | IOException e) {
            e.printStackTrace();
        }
    }
}
```

**5. Validating an XML Document with a Custom Validator**

```java
import javax.xml.validation.Validator;
import javax.xml.validation.ValidatorFactory;
import org.xml.sax.SAXException;

public class ValidateXMLCustom {

    public static void main(String[] args) {
        try {
            ValidatorFactory factory = ValidatorFactory.newInstance();
            Validator validator = factory.newValidator(new MyValidator());
            validator.validate(new InputSource("document.xml"));
            System.out.println("Document is valid");
        } catch (SAXException | IOException e) {
            e.printStackTrace();
        }
    }

    private static class MyValidator extends ValidatorHandler {

        @Override
        public void validate(SAXSource source) throws SAXException {
            // Custom validation logic
        }
    }
}
```

**6. Validating an XML Document with a JAXP Validator**

```java
import javax.xml.validation.Validator;
import javax.xml.validation.ValidatorFactory;
import org.xml.sax.SAXException;

public class ValidateXMLJAXP {

    public static void main(String[] args) {
        try {
            ValidatorFactory factory = ValidatorFactory.newInstance();
            Validator validator = factory.newValidator();
            validator.validate(new InputSource("document.xml"));
            System.out.println("Document is valid");
        } catch (SAXException | IOException e) {
            e.printStackTrace();
        }
    }
}
```

**7. Validating an XML Document with a DOM Validator**

```java
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class ValidateXMLDOM {

    public static void main(String[] args) {
        try {
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new File("schema.xsd"));
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document document = builder.parse("document.xml");
            Validator validator = schema.newValidator();
            validator.validate(document);
            System.out.println("Document is valid");
        } catch (SAXException | ParserConfigurationException | IOException e) {
            e.printStackTrace();
        }
    }
}
```

**8. Validating an XML Document with a Saxon Validator**

```java
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.XsltCompiler;
import net.sf.saxon.s9api.XsltExecutable;
import net.sf.saxon.s9api.XsltTransformer;
import org.xml.sax.SAXException;

public class ValidateXMLSaxon {

    public static void main(String[] args) {
        try {
            Processor processor = new Processor(false);
            XsltCompiler compiler = processor.newXsltCompiler();
            XsltExecutable executable = compiler.compile(new File("schema.xsl"));
            XsltTransformer transformer = executable.load();
            transformer.setParameter(new QName("schema"), new URL("file:schema.xsd"));
            transformer.transform(new Source("document.xml"), new Result("console"));
            System.out.println("Document is valid");
        } catch (SAXException | IOException e) {
            e.printStackTrace();
        }
    }
}
```

**9. Validating an XML Document with a Xerces Validator**

```java
import org.apache.xerces.jaxp.validation.XMLSchemaFactory;
import org.apache.xerces.jaxp.validation.XSGrammarPoolContainer;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.apache.xerces.xni.parser.XMLParserConfiguration;
import org.apache.xerces.xs.XSModel;
import org.apache.xerces.xs.XSParser;

public class ValidateXMLXerces {

    public static void main(String[] args) {
        try {
            XMLSchemaFactory factory = new XMLSchemaFactory();
            XSGrammarPoolContainer grammarPoolContainer = factory.newXSGrammarPoolContainer();
            XSParser parser = grammarPoolContainer.getXSModel().getSchema("schema.xsd");
            parser.validate(new XMLInputSource("document.xml"));
            System.out.println("Document is valid");
        } catch (IOException | SAXException e) {

```
