[英]How to format the XML schema validation error message in Java?
I want the default error messages printed when validating a xml file against it's schema to be set in a specific format. 我希望以特定格式设置针对其架构验证xml文件时打印的默认错误消息。
I do not want the schema error codes to be included in the error message. 我不希望在错误消息中包含架构错误代码 。 ie, cvc-complex-type-2-4-b, cvc-mininclusive-valid etc., 即cvc-complex-type-2-4-b,cvc-mininclusive-valid等,
I do not want the namespace to be printed in the error messages. 我不希望在错误消息中打印名称空间 。 eg, One of '{" http://sample.org/xml_schema ":schema_version}' is expected. 例如,应使用'{“ http://sample.org/xml_schema”:schema_version }'中的一个。 Note - The XML and XSD use Namespaces. 注-XML和XSD使用命名空间。
For example, 例如,
Default error message : 默认错误消息:
"cvc-complex-type.2.4.a: Invalid content was found starting with element 'report_version'. One of '{" http://sample.org/xml_schema ":schema_version}' is expected." “ cvc-complex-type.2.4.a:发现从元素'report_version'开始的无效内容。应为'{“ http://sample.org/xml_schema”:schema_version }'之一。
Expected error message : 预期的错误消息:
"Invalid content was found starting with element 'report_version'. One of '{schema_version}' is expected." “发现从元素'report_version'开始的内容无效。应该是'{schema_version}'之一。”
Code : 代码:
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) {
}
Let me know if there is a way to achieve this. 让我知道是否有办法实现这一目标。
I think it is not possible to override the Expception itself (besides maybe deriving from it and somehow override the throw function, but humm...). 我认为不可能覆盖Expception本身(可能从它派生而以某种方式覆盖throw函数,但是哼哼……)。 Because getMessage() returns a String (possibly null?) you can try to format it by yourself before printing. 因为getMessage()返回一个String(可能为null?),所以您可以尝试在打印之前自行格式化它。
There are two ':' so you can cut off everyting in front of the first ':', also you can throw everyting between " ": into the waste bin as far as the message you posted is concerned. 有两个':',因此您可以切断第一个':'前面的所有内容,也可以将“”:之间的所有内容扔到垃圾箱中,直到您发布的消息为止。 Keeping this in mind I am not sure if ALL SaxParseException look like this, you should give it a try. 请记住,我不确定是否所有的SaxParseException都是这样,您应该尝试一下。
Idea-Set: 理念 - 设置:
StringBuilder b = exception.getMessage();
b.delete(0, b.indexOf(":"));
b.replace(b.indexOf("{"), b.indexOf("":"), "{"); (you maybe must excape the " with \)
System.out.println(b.toString());
Hope this helps, feel free to ask. 希望这会有所帮助,随时问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.