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

Comments

Popular posts from this blog

PrimeFaces Push with Atmosphere

JavaOne 2015 Session: EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples by Gohar

Adding MyBatis mapper files dynamically