lunedì, febbraio 23, 2009

Validate xml against the schema

The code is a an adjustment of what published in this post, just using InputStream instead of File as arguments for schema and xml.

public static void validateXMLAgainstSchema(InputStream xmlStream, InputStream schemaStream) throws SAXException, SAXParseException, ParserConfigurationException, IOException {

SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
schemaFactory.setErrorHandler( new DefaultHandler());
Schema schemaXSD = schemaFactory.newSchema(new StreamSource(schemaStream));
Validator validator = schemaXSD.newValidator();
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(xmlStream);
validator.validate( new DOMSource( document));
}

Nessun commento: