*PROBLEM: how to validate parsed XML document against (parsed) XML-schema.
*IN: XML document as org.w3c.dom.Document, XML-schema as stream
*OUT: information if document is valid against schema. If document is not
valid DETAILED information about ALL errors (especially: node that caused
- validation is performed quite often,
- XML-document object is changed quite often,
- XML-Schema is fixed.
- XML-document doesn't contain information about related XML-schema
- XML-Schema is available as file only when the application starts.
It means that schema can be loaded from a file only once.
- must works in Java1.4+
I've tried to implement it using JAXP:
SchemaFactory schemaFactory =
Source schemaSource = new StreamSource(new File(schemaFile));
Schema schema = schemaFactory.newSchema(schemaSource);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlFile);
//document is being changed usign .addChild etc.
Validator validator = schema.newValidator();
Unfortunately org.xml.sax.ErrorHandler gets only SAXParseException which
doesn't constant information about node that caused the problem.
Is there any other way to validated DOM-document?