f



Validating parsed XML document against XML-schema

*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 
problem).

*Additional information:

-         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 
(attr xsi:noNamespaceSchemaLocation)

-         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:

//app start

SchemaFactory schemaFactory =

            SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

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();

validator.setErrorHandler(new SimpleErrorHandler());

validator.validate(new DOMSource(document));



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?


0
12/7/2005 4:53:55 PM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

1 Replies
693 Views

Similar Articles

[PageSpeed] 22

Well. I use XmlBeans...
http://xmlbeans.apache.org/
I'ts quite faster than DOM, and offers XSD validation.

Zechin

0
rodrigo.zr (14)
12/8/2005 2:01:40 PM
Reply: