Two questions:

1)      How to use xerces_2_9_0 rather than the default schema parser of
java 6 ?

2)       Try to use jaxp 1.3 APIs to perform schema validation, but  same
error was reported twice.  How to fix it?

          The error was 

Line:3 Column:77 cvc-datatype-valid.1.2.1: '123a' is not a valid value for
'integer'.

Line:3 Column:77 cvc-type.3.1.3: The value '123a' of element 'number' is not
valid.

 

The codes are listed as below:

      SchemaFactory factory =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema";);

      File schemaLocation = new File(schemaPath);

      try{

            Schema schema = factory.newSchema(schemaLocation);

            Validator validator = schema.newValidator();

            ErrorHandler lenient = new ForgivingErrorHandler();

            validator.setErrorHandler(lenient);

            Source source = new StreamSource(input);

        

            validator.validate(source);

     

      }  

      catch(Exception e){

            System.out.println("ex "+e);

}

 

public class ForgivingErrorHandler implements ErrorHandler {

      static Logger logger =
Logger.getLogger(ForgivingErrorHandler.class.getName());

      private ArrayList<String> errors;

      public void warning(SAXParseException ex) {

             String msg=getFormattedMsg(ex);

             putError(msg);

                }

 

    public void error(SAXParseException ex)  {

       String msg=getFormattedMsg(ex);

       putError(msg);

       System.out.println(msg);

    }

    public void fatalError(SAXParseException ex) throws SAXException{

      throw ex;

    }

    private String getFormattedMsg(SAXParseException ex){

      StringBuffer strBuff=new StringBuffer();

      strBuff.append("Line:");

      strBuff.append(ex.getLineNumber());

      strBuff.append(" Column:");

      strBuff.append(ex.getColumnNumber());

      strBuff.append(" ");

      strBuff.append(ex.getMessage());

      return strBuff.toString();

    }

    private void  putError(String msg){

      if(errors ==null){

            errors=new ArrayList<String>();

      }

      errors.add(msg);

    }

    public ArrayList<String> getErrors(){

      return errors;

    }

 

Reply via email to