Example usage for javax.xml.validation Validator validate

List of usage examples for javax.xml.validation Validator validate

Introduction

In this page you can find the example usage for javax.xml.validation Validator validate.

Prototype

public void validate(Source source) throws SAXException, IOException 

Source Link

Document

Validates the specified input.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String schemaLang = "http://www.w3.org/2001/XMLSchema";
    SchemaFactory factory = SchemaFactory.newInstance(schemaLang);
    Schema schema = factory.newSchema(new StreamSource("sample.xsd"));
    Validator validator = schema.newValidator();
    validator.validate(new StreamSource("sample.xml"));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
    Document document = parser.parse(new File("NewFile.xml"));

    Schema schema = schemaFactory.newSchema(new File("NewFile.xsd"));
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(document));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Customer customer = new Customer();
    customer.setName("abcabcabcabcabcabcabc");
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());

    JAXBContext jc = JAXBContext.newInstance(Customer.class);
    JAXBSource source = new JAXBSource(jc, customer);

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("customer.xsd"));

    Validator validator = schema.newValidator();
    validator.setErrorHandler(new MyErrorHandler());
    validator.validate(source);
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    File documentFile = new File(args[0]);
    File schemaFile = new File(args[1]);

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    Schema schema = null;/*from   ww w  . ja  va2  s.co  m*/
    try {
        schema = factory.newSchema(schemaFile);
    } catch (SAXException e) {
        fail(e);
    }

    Validator validator = schema.newValidator();

    SAXSource source = new SAXSource(new InputSource(new FileReader(documentFile)));

    try {
        validator.validate(source);
    } catch (SAXException e) {
        fail(e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    System.out.println("schema factory instance obtained is " + sf);

    Schema schema = sf.newSchema(new File(args[0]));
    System.out.println("schema obtained is = " + schema);
    Validator validator = schema.newValidator();

    String fileName = args[1].toString();
    String fileName2 = args[2].toString();
    javax.xml.transform.Result xmlResult = new javax.xml.transform.stax.StAXResult(
            XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(fileName2)));
    javax.xml.transform.Source xmlSource = new javax.xml.transform.stax.StAXSource(getXMLEventReader(fileName));
    validator.validate(new StreamSource(args[1]));
    validator.validate(xmlSource, xmlResult);

}

From source file:Main.java

public static void validate(Document doc, URL xsdLocation) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = factory.newSchema(xsdLocation);
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(doc));
}

From source file:Main.java

static boolean validateAgainstXSD(InputStream xml, InputStream xsd) {
    try {//from w  w w. ja  va2 s .  c  om
        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

/**
 * Perform a schema validation/*from w  ww. j  a  va  2  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 boolean validateXMLSchema(String xsdPath, String xmlPath) {

    try {//from  w w  w.ja va 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;
}

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 ww . j  a  v  a2 s .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);
    }
}