public static Document parseXmlStringWithXsdValidation ( String _xmlStr , boolean _namespaceAware , ErrorHandler _errorHandler ) throws IOException { if ( _errorHandler == null ) { _errorHandler = new XmlErrorHandlers . XmlErrorHandlerQuiet ( ) ; } DocumentBuilderFactory dbFac = DocumentBuilderFactory . newInstance ( ) ; dbFac . setValidating ( true ) ; dbFac . setNamespaceAware ( _namespaceAware ) ; dbFac . setAttribute ( "http://java.sun.com/xml/jaxp/properties/schemaLanguage" , "http://www.w3.org/2001/XMLSchema" ) ; try { DocumentBuilder builder = dbFac . newDocumentBuilder ( ) ; builder . setErrorHandler ( _errorHandler ) ; return builder . parse ( new ByteArrayInputStream ( _xmlStr . getBytes ( StandardCharsets . UTF_8 ) ) ) ; } catch ( IOException _ex ) { throw _ex ; } catch ( Exception _ex ) { throw new IOException ( "Failed to parse " + StringUtil . abbreviate ( _xmlStr , 500 ) , _ex ) ; } }
Loads XML from string and uses referenced XSD to validate the content .