Sunday, March 31, 2013

How to validate XML file against XSD in Java


private boolean validateAgainstXSD(InputStream xml, InputStream xsd) {
 try {
         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;
 }
}

No comments:

Post a Comment