Skip to main content

How to get required mandatory field missing in xml using saxparser

No replies
james_georgee
Offline
Joined: 2008-07-25
Points: 0

Hi Friends,
I am getting the following error
Error at 8 line.
The content of element type "note" must match "(to+,from+,heading,body)".

I have removed the to portion from the xml which is mandatory as per the dtd, the error message does not show that which field is missing, just displaying that there are some problems in the note elements.

Is there any way i get the explicit error messages, saying that to is missing etc..

Java sample code i written..
public class DOMValidateDTD {
public static void main(String args[]) {
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new org.xml.sax.ErrorHandler() {
//Ignore the fatal errors
public void fatalError(SAXParseException exception)throws SAXException { }
//Validation errors
public void error(SAXParseException e)throws SAXParseException {
System.out.println("Error at " +e.getLineNumber() + " line.");
System.out.println(e.getMessage());
System.exit(0);
}
//Show warnings
public void warning(SAXParseException err)throws SAXParseException{
System.out.println(err.getMessage());
//System.exit(0);
}
});
Document xmlDocument = builder.parse(new FileInputStream("Employeexy.xml"));
DOMSource source = new DOMSource(xmlDocument);
StreamResult result = new StreamResult(System.out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "Employee.dtd");
transformer.transform(source, result);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

Thanks
James George