[英]How to dissemble a SaxParseException (cvc-pattern-valid)?
What is the best way to handle validation errors of xml-data against an xs:simpleType with an xs:pattern? 处理具有xs:pattern的xs:simpleType的xml数据的验证错误的最佳方法是什么?
eg a validation against 例如针对
<xs:simpleType name="IBANIdentifier">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z]{2,2}[0-9]{2,2}[a-zA-Z0-9]{1,30}"/>
</xs:restriction>
</xs:simpleType>
would lead to this SaxParseException: 会导致此SaxParseException:
org.xml.sax.SAXParseException: cvc-pattern-valid: Value 'XXAA99999999999911' is
not facet-valid with respect to pattern '[a-zA-Z]{2,2}[0-9]{2,2}[a-zA-Z0-9]{1,30}
for type 'IBANIdentifier'.
In my DefaultHandler.error(), how can I access the 在我的DefaultHandler.error()中,如何访问
without parsing the error-msg? 没有解析错误味精?
The exception does not hold this information in a structured form, AFAIKS. 异常不会以结构化形式AFAIKS保存此信息。 The parser has some state, but I dont find the information I need.
解析器具有某种状态,但是我找不到所需的信息。
You can capture all the validation errors/warnings by writing your own error handler and registering with the Validator using validator.setErrorHandler(new CollectingErrorHandler(errors, shouldConsiderWarningsAsErrors));
您可以通过编写自己的错误处理程序并使用Validator.setErrorHandler
validator.setErrorHandler(new CollectingErrorHandler(errors, shouldConsiderWarningsAsErrors));
在验证validator.setErrorHandler(new CollectingErrorHandler(errors, shouldConsiderWarningsAsErrors));
注册来捕获所有验证错误/警告validator.setErrorHandler(new CollectingErrorHandler(errors, shouldConsiderWarningsAsErrors));
The CollectingErrorHandler
will now have access to all the validation errors. 现在,
CollectingErrorHandler
将可以访问所有验证错误。 Now, there is no standard format for reporting these errors and might change from parser implementation. 现在,没有报告这些错误的标准格式,并且可能会从解析器实现中更改。 So stick to one parser implementation and use regex for parsing.
因此,请坚持使用一种解析器实现,并使用正则表达式进行解析。
public final class CollectingErrorHandler implements ErrorHandler
{
private final List<SAXException> l;
private final boolean warningsAreErrors;
public CollectingErrorHandler(final List<SAXException> l, final boolean shouldConsiderWarningsAsErrors)
{
this.l = l;
warningsAreErrors = shouldConsiderWarningsAsErrors;
}
public void error(final SAXParseException e)
{
l.add(e);
}
public void fatalError(final SAXParseException e)
{
l.add(e);
}
public void warning(final SAXParseException e)
{
if (warningsAreErrors)
{
l.add(e);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.