简体   繁体   English

如何分解SaxParseException(cvc-pattern-valid)?

[英]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()中,如何访问

  • pattern 图案
  • actual value 实际价值
  • name of the simple type 简单类型的名称

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.

相关问题 如何解决cvc-pattern-valid:就模式问题而言,值”不是有效方面 - How to resolve cvc-pattern-valid: Value '' is not facet-valid with respect to pattern problem SAXParseException:值不是“日期”的有效值 - SAXParseException: value is not a valid value for 'date' 如何解决 Jenkins 中的 SAXParseException? - How to solve the SAXParseException in Jenkins? org.xml.sax.SAXParseException; lineNumber:6; columnNumber:122; cvc-elt.1:找不到元素&#39;beans&#39;的声明 - org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 122; cvc-elt.1: Cannot find the declaration of element 'beans' JAXB解组架构问题:“ org.xml.sax.SAXParseException cvc-elt.1” - JAXB unmarshalling schema issues: 'org.xml.sax.SAXParseException cvc-elt.1' org.xml.sax.SAXParseException:cvc-elt.1:找不到元素'tns:root_element'的声明 - org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'tns:root_element' org.xml.sax.SAXParseException:cvc-complex-type.2.4.c:匹配的通配符是strict - org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict Spring上下文错误:SAXParseException cvc-complex-type.3.2.2忽略无法解决的上下文:属性占位符 - Spring context error: SAXParseException cvc-complex-type.3.2.2 ignore-unresolvable context:property-placeholder 错误:org.xml.sax.SAXParseException:cvc-complex-type.2.3 - Erro: org.xml.sax.SAXParseException: cvc-complex-type.2.3 XML解析异常org.xml.sax.SAXParseException cvc-elt.1 - XML parsing exception org.xml.sax.SAXParseException cvc-elt.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM