验证 xml。获取抛出 cvc-enumeration-valid 的元素名称

Validating xml. Get the element name that throws cvc-enumeration-valid

大家早上好!

我正在以这种方式针对 xsd 验证 xml:

ValidationEventCollector vec;
    URL xsdUrl;
    Schema schema;
    FicheroIntercambio fichero;

    vec = new ValidationEventCollector();
    try{
        xsdUrl = FicheroIntercambio.class.getResource("xsd/file.xsd");
        SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        schema = sf.newSchema(xsdUrl);
        jaxbContext = JAXBContext.newInstance("file.dto");
        unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(vec);
        bais = new ByteArrayInputStream(peticion.getBytes("UTF-8"));
        fichero = (FicheroIntercambio)unmarshaller.unmarshal(bais);
        bais.close();
    }catch(Exception ex){
        String validacionXml="";
        if(vec!=null && vec.hasEvents()){
            for(ValidationEvent ve:vec.getEvents()){
                validacionXml += ve.getMessage();
            }
        }else{
            validacionXml += ex.getLocalizedMessage();
        }

    }

xsd 的一部分是:

<xs:element minOccurs="1" maxOccurs="1" name="Indicador_Prueba">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:maxLength value="1"/>
                <xs:enumeration value="0"/>
                <xs:enumeration value="1"/>
              </xs:restriction>
            </xs:simpleType>
          </xs:element>

它工作完美,它验证了一切。问题是当它启动这个枚举异常时,它说的是这样的:

cvc-enumeration-valid: Value ''{0}'' is not facet-valid with respect to enumeration ''{1}''. It must be a value from the enumeration

是否可以获取抛出异常的元素?至少得到元素类型?

提前感谢大家!

最后,我没有使用获取错误但处于低级别的 ValidationEventCollector,而是使用了 Validator 并使用错误处理程序来控制错误。现在我得到了 cvc-enumeration-valid 错误并得到了抛出它的类型。 这是一个例子:

url = FILE.class.getResource("xsd/file.xsd");
        SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        schema = sf.newSchema(xsdUrl);
        Validator validator = schema.newValidator();
        final List<SAXParseException> exceptions = new LinkedList<SAXParseException>();
        validator.setErrorHandler(new ErrorHandler() {

            public void fatalError(SAXParseException exception) throws SAXException {
                // TODO Auto-generated method stub
                exceptions.add(exception);
            }

            public void error(SAXParseException exception) throws SAXException {
                // TODO Auto-generated method stub
                exceptions.add(exception);
            }
        });

        bais = new ByteArrayInputStream(peticion.getBytes("UTF-8"));

        //validate the xml against the xsd
        validator.validate(new StreamSource(bais));
        if(!exceptions.isEmpty()){
            for(SAXParseException ex:exceptions){
                validacionXml += ex.getMessage();
            }
        }

启动验证方法后,errorHandler 会存档所有错误、致命错误或警告(如果您想要获取启动错误的类型)。

感谢大家