I have searched online and tried for few days but I still can't list out all the errors, I only can show 1 error. Why? I need to list out all the error. Hope someone help me. Below is my validate and error handler code. The program goes to error() only, and only once. It error for the first element but there are other elements missing in the XML as well, it does not display them.
import java.io.File; import java.io.IOException; import javax.xml.XMLConstants; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.xml.sax.SAXException; public class XMLValidate1 { public static void main(String[] args) throws SAXException, IOException { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); File schemaLocation = new File("Q1.xsd"); Schema schema = factory.newSchema(schemaLocation); Validator validator = schema.newValidator(); validator.setErrorHandler(new ValidateErrorHandler()); Source xmlFile = new StreamSource(new File("Q1.xml")); try { validator.validate(xmlFile); System.out.println(xmlFile.getSystemId() + " is valid."); } catch (SAXException ex) { System.out.println(xmlFile.getSystemId() + " is not valid because "); System.out.println(ex.getMessage()); } } } Below is my errorhandler
import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public class ValidateErrorHandler implements ErrorHandler { public void warning(SAXParseException ex) { System.out.println("Warning: "); System.err.println(ex.getMessage()); } public void error(SAXParseException ex) { System.out.println("Error: "); System.err.println(ex.getMessage()); } public void fatalError(SAXParseException ex) throws SAXException { System.out.println("Fatal error: "); System.err.println(ex.getMessage()); } }