I use JAXP to do XML Parsing and validation. I use DOM and not SAX.
My code is as follows:
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
File schemaLocation = new File("C:\\config\\JMS_properties.xsd");
Schema schema = schemaFactory.newSchema(schemaLocation);
// Create the validator
Validator validator = schema.newValidator();
// Add an error handler to the validator
JMSErrorHandler errorHandler = new JMSErrorHandler();
validator.setErrorHandler(errorHandler);
// Create the dom factory
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
// Set the namespace property
domFactory.setNamespaceAware(true);
// Create the document builder
DocumentBuilder builder = domFactory.newDocumentBuilder();
// Parse the xml file
Document doc = builder.parse(xmlFile);
// Create the dom source and destination
// The destination will contain the doc augmented with the default attribute/element
DOMSource source = new DOMSource(doc);
DOMResult result = new DOMResult();
// Validate and augment the source
validator.validate(source, result);
// Error checking
if ( errorHandler.validationError == true ) {
// errors occured during the parsing
System.out.println("XML file is not valid");
System.exit(1);
}
// Get the augmented document
this.augmented = (Document) result.getNode();
When executing my code, I got:
Validation error: cvc-elt.1: Cannot find the declaration of element 'JMSProperties'
But the xml file has this element in it so I do not understand the meaning of that error.
??????