javax.xml.validation


1. Validating an XML Document with a Schema

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

3. Validating an XML Document with a Relax NG Schema

4. Validating an XML Document with a Schematron Schema

5. Validating an XML Document with a Custom Validator

6. Validating an XML Document with a JAXP Validator

7. Validating an XML Document with a DOM Validator

8. Validating an XML Document with a Saxon Validator

9. Validating an XML Document with a Xerces Validator