[jboss-dev] Validating XML documents
Emmanuel Bernard
emmanuel at hibernate.org
Wed Dec 16 14:11:38 EST 2009
Sorry for the stupid question but how do we validate XML InputStream that are following an XSD?
I've got something like
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setValidating( true ); //apparently useless cause it's for DTDs only
docBuilderFactory.setNamespaceAware( true );
//this one seems to do nothing either
docBuilderFactory.setSchema(
SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI )
.newSchema( new StreamSource( getStreamFromClasspath( "persistence_1_0.xsd" ) ) ) );
InputSource source = new InputSource( is );
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.setEntityResolver( resolver );
docBuilder.setErrorHandler( new ErrorLogger( "XML InputStream", errors, resolver ) );
Document doc = docBuilder.parse( source );
if ( errors.size() != 0 ) {
throw new PersistenceException( "invalid persistence.xml", (Throwable) errors.get( 0 ) );
}
But even with illegal elements in the XML (well-formed though), I get no error :(
More information about the jboss-development
mailing list