我希望在针对其架构验证 xml 文件时打印的默认错误消息以特定格式设置。
我不希望架构错误代码包含在错误消息中。即,cvc-complex-type-2-4-b、cvc-mininclusive-valid 等,
我不希望在错误消息中打印名称空间。例如,需要 '{" http://sample.org/xml_schema ":schema_version}' 之一。注意 - XML 和 XSD 使用命名空间。
例如,
默认错误消息:
“cvc-complex-type.2.4.a:发现以元素“report_version”开头的无效内容。需要“{” http://sample.org/xml_schema“:schema_version }”之一。”
预期的错误消息:
“发现以元素 'report_version' 开头的无效内容。需要 '{schema_version}' 之一。”
代码 :
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(ClassLoader.getSystemResource("sample_schema.xsd"));
factory.setSchema(schema);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) {
System.out.println(exception.getMessage());
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
System.out.println(exception.getMessage());
}
@Override
public void error(SAXParseException exception) {
System.out.println(exception.getMessage());
}
});
doc = builder.parse(inputSource);
} catch(Exception e) {
}
让我知道是否有办法实现这一目标。