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

public static void validateSChema(String xmlFile, String... xsdFiles) throws SAXException, IOException {

    Source[] schemas = new Source[xsdFiles.length];
    for (int i = 0; i < schemas.length; i++) {
        schemas[i] = new StreamSource(xsdFiles[i]);
    }/*  w  w  w  . j  a  va2s  .  c o m*/

    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = schemaFactory.newSchema(schemas);
    Validator validator = schema.newValidator();
    Source source = new StreamSource(xmlFile);
    validator.validate(source);
}

From source file:Main.java

/**
 * Perform a schema validation/*from   w  w  w .  j  a  v  a2 s  .  c  o m*/
 */
public static void validate(Source schema, Source document) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema sch = factory.newSchema(schema);
    Validator validator = sch.newValidator();
    validator.validate(document);
}

From source file:Main.java

public static void validate(Schema schema, Document document) {

    Validator validator = schema.newValidator();
    try {/*from  w  w  w.  j  a v a 2s . c o  m*/
        validator.validate(new DOMSource(document));
    } catch (Exception e) {
        throw new RuntimeException("Failed to validate document", e);
    }
}

From source file:Main.java

static boolean validateAgainstXSD(InputStream xml, InputStream xsd) {
    try {/*from w  w  w. j  a va  2  s  . co m*/
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new StreamSource(xsd));
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(xml));
        return true;
    } catch (Exception ex) {
        return false;
    }
}

From source file:Main.java

/**
 * Validate xml file using specific schema
 * @param sourceXmlBytes//from  www  .  ja  v a  2s  . c  o  m
 *        the byte array reading from xml file
 * @param schemaUrl   
 *         schema url used for validation
 * @return  byte[]
 */
public static void validate(byte[] sourceXmlBytes, URL schemaUrl) throws IOException, SAXException {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
        Schema schema = factory.newSchema(schemaUrl);
        Validator validator = schema.newValidator();
        Source source = new StreamSource(new ByteArrayInputStream(sourceXmlBytes));
        validator.validate(source);
    } catch (SAXException ex) {
        throw ex;
    } catch (IOException e) {
        throw e;
    }
}

From source file:Main.java

public static boolean validateXMLString(String xsdPath, String xml) {
    try {//from w  w w .j a v  a2 s.c  om
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new File(xsdPath));
        Validator validator = schema.newValidator();
        Source source = new StreamSource(new StringReader(xml));

        validator.validate(source);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean doesXMLMatchXSD(String xmlUrl) throws SAXException, FileNotFoundException, IOException {

    boolean doesMatchXSD = false;

    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    SAXSource sourceXSD = new SAXSource(new InputSource(new FileInputStream(new File(xsdUrl))));
    Schema schema = factory.newSchema(sourceXSD);
    Validator validator = (Validator) schema.newValidator();
    validator.validate(new StreamSource(new File(xmlUrl)));

    doesMatchXSD = true;//from w ww.  j  a v a  2 s.  c  o  m
    return doesMatchXSD;
}

From source file:Main.java

public static String validate(String xsdPath, String xmlPath) {
    String response = "OK";

    try {//from  w  w w .j ava  2 s  .c o  m
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        File schemaLocation = new File(xsdPath);
        Schema schema = factory.newSchema(schemaLocation);
        Validator validator = schema.newValidator();
        Source source = new StreamSource(xmlPath);
        Result result = null;
        validator.validate(source, result);
    } catch (Exception e) {
        response = e.getMessage();
    }

    return response;
}

From source file:Main.java

public static void validate(String xmlFileName, String schemaFileName)
        throws IOException, ParserConfigurationException, SAXException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(new File(schemaFileName));
    Validator validator = schema.newValidator();

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    DocumentBuilder parser = builderFactory.newDocumentBuilder();
    Document document = parser.parse(new File(xmlFileName));
    validator.validate(new DOMSource(document));
}

From source file:Main.java

public static boolean validateXMLSchema(String xsdPath, String xmlPath) {

    try {/*from w w  w.  java 2 s.  c o  m*/
        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 File(xmlPath)));
    } catch (IOException e) {
        System.out.println("Exception: " + e.getMessage());
        return false;
    } catch (SAXException e) {
        System.out.println("Exception: " + e.getMessage());
        return false;
    }
    return true;
}