Mega Code Archive

 
Categories / Java / XML
 

Create XML validator from XML schema

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 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"));   } }