Monday, 30 January 2012

Validate xml use XSD

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

public void validateXml(String xsdFile, String xmlFile) {
   try {
      final String schemaURI = XMLConstants.W3C_XML_SCHEMA_NS_URI; 
      SchemaFactory factory = SchemaFactory.newInstance(schemaURI); 
      Schema schema = factory.newSchema(new StreamSource(xsdFile)); 
      Validator validator = schema.newValidator();
      validator.validate(new StreamSource(xmlFile));
   } catch(Exception e) {
     System.err.println(e);
   }
}

No comments:

Post a Comment