Hi, I am working on a project that will validate an xml Document object from a child class. I seem to have completed XSD validation with new DOMSource(doc) and using schema factory, which doesn't support DTD validation. And now I am attempting to validate the DTD. It seems like all the examples I've seen are trying to parse files, and I am struggling with finding a way to validate it directly against the document object.
The child class creates the xml document which is passed back to the parent class, and I need to do the validation in the parent class, so I need away to validate against the document object. This will be an assertion in Fitnesse if anyone is familar with it.
private void doSchemaValidationCommand(int rowCount, Document doc, String xpathExpression)
{
String validationPath = getText(rowCount,1);
String extensionSeparator = ".";
Boolean error = false;
String ext = null;
try
{
int dot = validationPath.lastIndexOf(extensionSeparator);
ext = validationPath.substring(dot + 1);
}
catch(Exception e)
{
this.wrong(rowCount, 1, e.getMessage());
}
if (ext.equalsIgnoreCase("xsd"))
{
try {
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
File schemaLocation = new File(validationPath);
Schema schema = factory.newSchema(schemaLocation);
Validator validator = schema.newValidator();
try {
validator.validate(new DOMSource(doc));
}
catch (SAXException ex) {
this.wrong(rowCount, 1, ex.getMessage());
error = true;
}
catch (IOException ex) {
this.wrong(rowCount, 1, ex.getMessage());
error = true;
}
}
catch (SAXException ex) {
this.wrong(rowCount, 1, ex.getMessage());
error = true;
}
catch (Exception ex){
this.wrong(rowCount, 1, ex.getMessage());
error = true;
}
}
else if (ext.equalsIgnoreCase("dtd"))
{
try {
}
}