title: how to write custom error handler for xsd validation for jaxb object
body: <p>I want validate my JAXB object using xsd And want to return proper error message. But the error message i m able to get is :</p>
  
  <blockquote>
    <p>cvc-complex-type.2.4.a: Invalid content was found starting with
    element 'applicant-dto'. One of '{applicant-id}' is expected.</p>
    
    <p>[cvc-pattern-valid: Value '12345670' is not facet-valid with respect
    to pattern '^\d{3}-\d{2}-\d{4}$' for type 'ssn'., cvc-type.3.1.3:
    The value '12345670' of element 'ssn' is not valid.]</p>
  </blockquote>
  
  <p>I want to get proper error message. Can anybody help me out to figure out same.</p>
  
  <p>my xsd is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  
    &lt;xs:element name="applicant" type="applicantDTO" /&gt;
  
    &lt;xs:complexType name="applicantDTO" &gt;
      &lt;xs:sequence&gt;
        &lt;xs:element name="applicant-id" type="xs:long" minOccurs="1" /&gt;
        &lt;xs:element name="first-name" type="name" minOccurs="0" /&gt;
        &lt;xs:element name="middle-name" type="xs:string" minOccurs="0" /&gt;
        &lt;xs:element name="last-name" type="name" minOccurs="0" /&gt;
        &lt;xs:element name="ssn" type="ssn" minOccurs="1" /&gt;
        &lt;xs:element name="date-of-birth" type="xs:string" minOccurs="0" /&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:complexType&gt;
  
    &lt;xs:simpleType name="name"&gt;
      &lt;xs:restriction base="xs:string"&gt;
          &lt;xs:pattern value="[a-zA-Z ]{3,30}"&gt;&lt;/xs:pattern&gt;
      &lt;/xs:restriction&gt;
    &lt;/xs:simpleType&gt;
    &lt;xs:simpleType name="ssn"&gt;
      &lt;xs:restriction base="xs:string"&gt;
          &lt;xs:pattern value="^\\d{3}-\\d{2}-\\d{4}$"&gt;&lt;/xs:pattern&gt;
      &lt;/xs:restriction&gt;
    &lt;/xs:simpleType&gt;
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>My JAXB Object is</p>
  
  <blockquote>
    <p>ApplicantDTO</p>
  </blockquote>
  
  <p>for validating it with XSD I m writing below code</p>
  
  <pre><code>@Test
      public void testValidateApplicantDTO() throws Exception {
          ApplicantDTO eDTO = getApplicantDTO();
          SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
          Schema schema = sf.newSchema(new File("src/test/resources/applicant.xsd")); 
  
          Validator validator =  schema.newValidator();
          CustomErrorHandler errorHandler = new CustomErrorHandler();
          validator.setErrorHandler(errorHandler);
  
          JAXBContext jc = JAXBContext.newInstance(ApplicantDTO.class);
          JAXBSource source = new JAXBSource(jc, eDTO);
          validator.validate(source);
  
          System.out.println(errorHandler.getErrors());
      }
  </code></pre>
  
  <p>I have written my custom error hadler</p>
  
  <p>CustomErrorHandler </p>
  
  <pre><code>public class CustomErrorHandler extends DefaultHandler  {
  
      List&lt;String&gt; errors = new ArrayList&lt;String&gt;();
  
      @Override
      public void warning(SAXParseException exception) throws SAXException {
          System.out.println("CustomErrorHandler.warning()");
      }
  
      @Override
      public void error(SAXParseException exception) throws SAXException {
          System.out.println("CustomErrorHandler.error()");
          errors.add(exception.getLocalizedMessage());
      }
  
      @Override
      public void fatalError(SAXParseException exception) throws SAXException {
          System.out.println("CustomErrorHandler.fatalError()");
      }
  
      @Override
      public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
          super.startElement(uri, localName, qName, attributes);
          System.out.println("CustomErrorHandler.startElement()");
      }
  
      @Override
      public void endElement(String uri, String localName, String qName) throws SAXException {
          super.endElement(uri, localName, qName);
          System.out.println("CustomErrorHandler.endElement()");
      }
  
      /**
       * @return the errors
       */
      public List&lt;String&gt; getErrors() {
          return this.errors;
      }
  
      /**
       * @param errors the errors to set
       */
      public void setErrors(List&lt;String&gt; errors) {
          this.errors = errors;
      }
  }
  </code></pre>
  
  <p>the error list return to me with error message :</p>