"Jing Yang" <[EMAIL PROTECTED]> wrote on 01/24/2008 01:51:28 PM:
> Two questions: > 1) How to use xerces_2_9_0 rather than the default schema > parser of java 6 ? Putting Xerces' jars (xml-apis.jar and xercesImpl.jar) on the classpath should work. Failing that you should use the Endorsed Standards Override Mechanism. More info here: http://java.sun.com/j2se/1.5.0 /docs/guide/standards/. > 2) Try to use jaxp 1.3 APIs to perform schema validation, but > same error was reported twice. How to fix it? They might look the same but they're violations of different schema constraints. Xerces reports an error for each. > 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; > } Michael Glavassevich XML Parser Development IBM Toronto Lab E-mail: [EMAIL PROTECTED] E-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]