Validate Document object against Schema without reparsing the xml

V

veerleverbr

Hi,

Is it possible to validate a Document object against a schema file
without reparsing the xml:

public void validate(org.w3c.dom.Document doc, File xsd) throws
SAXException {
???
}

Any suggestions?

Veerle
 
I

iksrazal

Is it possible to validate a Document object against a schema file
without reparsing the xml:

public void validate(org.w3c.dom.Document doc, File xsd) throws
SAXException {
???
}

Any suggestions?

Veerle

Just load the objects into a static variable, singleton etc. Use
EntityResolver for the schema. DefaultHandler is the way to go for the
validation. Try clarifying the problem if I mis-understand.

HTH,
iksrazal
www.braziloutsource.com
 
V

veerleverbr

I found it is possible to do such a thing using the
javax.xml.validation package of Java 1.5:

private class ValidationErrorHandler extends DefaultHandler {
private List exList = new ArrayList();
public SAXParseException getFirstSaxParseException() {
return
(exList.size()==0?null:(SAXParseException)exList.get(0));
}
public boolean validationError() {
return (exList.size()> 0);
}
public void error(SAXParseException exception) throws SAXException
{
exList.add(exception);
}
public void fatalError(SAXParseException exception) throws
SAXException {
exList.add(exception);
}
public void warning(SAXParseException exception) throws
SAXException {
}
}

public void validateDocument(Document doc, String xsd) throws
SAXException, IOException {
SchemaFactory factory = SchemaFactory.newInstance

(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile = new StreamSource(new File(xsd));
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
ValidationErrorHandler errHandler = new ValidationErrorHandler();
validator.setErrorHandler(errHandler);
validator.validate(new DOMSource(doc));
if (errHandler.validationError()) {
throw errHandler.getFirstSaxParseException();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top