Example usage for javax.xml.validation Schema newValidator

List of usage examples for javax.xml.validation Schema newValidator

Introduction

In this page you can find the example usage for javax.xml.validation Schema newValidator.

Prototype

public abstract Validator newValidator();

Source Link

Document

Creates a new Validator for this Schema .

Usage

From source file:Main.java

/**
 * This method will validate a non-niem xml instance.
 * /*w w  w  .  ja v  a  2s.co  m*/
 * @param xsdPath - this is a relative path to an xsd, typically in OJB_Utilies or in the enclosing project
 * @param xml - This is the XML document as a string
 * @throws Exception - typically a validation exception or a file path exception
 */

public static void validateInstanceNonNIEMXsd(String xsdPath, String xml) throws Exception {

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(new File(xsdPath));
    Validator validator = schema.newValidator();
    validator.validate(new StreamSource(new StringReader(xml)));
}

From source file:Main.java

public static boolean xmlStringValidate(String xmlStr, String xsdPath) {
    boolean flag = false;
    try {//  w ww .j av a  2 s .  co  m
        SchemaFactory factory = SchemaFactory.newInstance(SCHEMALANG);
        File schemaLocation = new File(xsdPath);
        Schema schema = factory.newSchema(schemaLocation);
        Validator validator = schema.newValidator();
        InputStream is = new ByteArrayInputStream(xmlStr.getBytes());
        Source source = new StreamSource(is);
        try {
            validator.validate(source);
            flag = true;
        } catch (SAXException ex) {
            System.out.println(ex.getMessage());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return flag;
}

From source file:Main.java

/**
 * Validates XML document using XSD schema.
 *
 * @param document     document to validate
 * @param pathToSchema path to document with XSD schema
 * @return {@code true} if document is valid
 *//*from   w  w w  . j ava2s  .  c om*/
public static boolean validate(Document document, String pathToSchema) {
    try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new File(pathToSchema));
        Validator validator = schema.newValidator();

        validator.validate(new DOMSource(document));
        return true;
    } catch (IOException | SAXException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static String isXmlPassingSchemaValidation(InputStream xml, InputStream xsd) throws IOException {
    Source xmlSource = new StreamSource(xml);

    try {//from   w  w w . j  a va2s.c  o m
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new StreamSource(xsd));
        Validator validator = schema.newValidator();
        validator.validate(xmlSource);
        return null;
    } catch (SAXException e) {
        return e.toString();
    }

}

From source file:Main.java

public static boolean validate(URL schemaFile, String xmlString) {
    boolean success = false;
    try {//from w  w  w. ja va 2 s . c om
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(schemaFile);
        Validator validator = schema.newValidator();
        Source xmlSource = null;
        try {
            xmlSource = new StreamSource(new java.io.StringReader(xmlString));
            validator.validate(xmlSource);
            System.out.println("Congratulations, the document is valid");
            success = true;
        } catch (SAXException e) {
            e.printStackTrace();
            System.out.println(xmlSource.getSystemId() + " is NOT valid");
            System.out.println("Reason: " + e.getLocalizedMessage());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return success;
}

From source file:Main.java

/**
 * Validates XML on a XMLschema/*  w w w.j  a  v  a  2 s.  co m*/
 * 
 * @param xmlSchema
 * @param sourceXml
 */
public static void validateXmlOnSchema(File xmlSchema, Source sourceXml) {
    Schema schema = newSchema(xmlSchema);
    try {
        schema.newValidator().validate(sourceXml);
    } catch (Exception e) {
        throw new RuntimeException(
                "Could not validate '" + sourceXml.getSystemId() + "' with '" + xmlSchema.getName() + "'!", e);
    }
}

From source file:Main.java

/**
 * Validates XML on a XMLschema/*from   w w  w. j a  v  a2 s . c o m*/
 * 
 * @param xmlSchema
 * @param sourceXml
 */
public static void validateXmlOnSchema(Source xmlSchema, Source sourceXml) {
    Schema schema = newSchema(xmlSchema);
    try {
        schema.newValidator().validate(sourceXml);
    } catch (Exception e) {
        throw new RuntimeException(
                "Could not validate '" + sourceXml.getSystemId() + "' with '" + xmlSchema.getSystemId() + "'!",
                e);
    }
}

From source file:io.konik.utils.InvoiceLoaderUtils.java

public static Validator getSchemaValidator() throws SAXException {
    SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
    URL schemaInvoice = notNull(InvoiceLoaderUtils.class.getResource(ZF_1_SCHEMA_XSD));
    Schema invoiceSchema = sf.newSchema(schemaInvoice);
    return invoiceSchema.newValidator();
}

From source file:Main.java

public static boolean validateXML(String schemaPath, String xmlPath) {
    try {/*ww  w  .  j ava 2  s. c o  m*/
        String schemaLang = "http://www.w3.org/2001/XMLSchema";
        SchemaFactory factory = SchemaFactory.newInstance(schemaLang);
        // create schema by reading it from an XSD file:
        Schema schema = factory.newSchema(new StreamSource(schemaPath));
        Validator validator = schema.newValidator();
        // at last perform validation:
        validator.validate(new StreamSource(xmlPath));
    } catch (Exception ex) {
        return false;
    }
    return true;
}

From source file:com.github.woozoo73.ht.XmlUtils.java

public static boolean validate(String xsd, String xml) {
    try {/*from  w  w  w .j  a  va2s  .c  o  m*/
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        Schema schema = factory.newSchema(new StreamSource(new ByteArrayInputStream(xsd.getBytes("UTF-8"))));
        Validator validator = schema.newValidator();

        boolean valid = false;
        try {
            validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes("UTF-8"))));
            valid = true;
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        }

        return valid;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}