XML Validation using SAX Parser

Hi,

I am trying to parse an XML using the SAX parser.
But when i run it i get File “DTD File Name” not found.

public static void validateUsingSAX(String xmldata) {
try {
InputStream is = new ByteArrayInputStream(xmldata.getBytes());
SAXParserFactory f = SAXParserFactory.newInstance();
f.setValidating(true);
SAXParser p = f.newSAXParser();
DefaultHandler h = new MyErrorHandler();
p.parse(is,h);
} catch (Exception e) {
errorMessage = errorMessage + " Message==> " + e.getMessage()+ “\n”;
}
}

private static class MyErrorHandler extends DefaultHandler {

public void warning(SAXParseException e) throws SAXException {
createErrorMessage(e);
}
public void error(SAXParseException e) throws SAXException {
createErrorMessage(e);
}
public void fatalError(SAXParseException e) throws SAXException {
createErrorMessage(e);
}

private void createErrorMessage(SAXParseException e) {
errorMessage = errorMessage + " Line number:" + e.getLineNumber() + " Message==> " + e.getMessage() + “\n”;
}
}

Thanks in advance,

Gamad

Does your xml file contain valid directory path to the DTD or XSD file?
By the way, there are multiple validation build-in services in webMethods. What is the reason for java validation? just for the SAX parser?

Hi Terry,

Here is a snippet of the xml that i am trying to validate

!DOCTYPE meeting_display SYSTEM “file:test1234.dtd”>

Note. leading < is removed since the formatting was getting messed.

I agree there are a few methods in webMethods that help you to validate. But whenevever there is a change in DTD i have to change the RECORD against which i am validating or the schema that i am validating against. But if i use a java code that uses a parser like SAX, all i have to do is move the modified DTD to the appropriate place.

Gamad

I see where you are getting at. Firstly, the example you are given is not a well-formed xml which should never made it to validation. Even it did, webMethods pharser could still pick up the error. I think if I understand you correctly, its a matter of convienence in the event of schema changing. From what I understand, schema should not be change often after production, even if it did, it should just be an easy delete then import procedure. By creating your own pharser might create a maintance and debuging issue in the future. Maybe just a little over-killed.

cheers