Java XML Validate validateXML(String schemaPath, String xmlPath)

Here you can find the source of validateXML(String schemaPath, String xmlPath)

Description

validate XML

License

Open Source License

Declaration

public static boolean validateXML(String schemaPath, String xmlPath) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

public class Main {
    public static boolean validateXML(String schemaPath, String xmlPath) {
        try {//w ww .j a v a 2 s . co 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;
    }
}

Related

  1. validateXml(final Schema schema, final byte[] xmlBytes)
  2. validateXml(InputStream xml, InputStream xsd)
  3. validateXML(String xml, String schemaURI, DefaultHandler handler)
  4. validateXMLFile(String filename, String schemaFile)
  5. validateXmlOnSchema(File xmlSchema, Source sourceXml)