[jboss-dev] Validating XML documents

Anil Saldhana Anil.Saldhana at redhat.com
Wed Dec 16 14:38:04 EST 2009


Emmanuel,
   http://java.sun.com/developer/technicalArticles/xml/validationxpath/

this article will help you.

On 12/16/2009 01:11 PM, Emmanuel Bernard wrote:
> 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