自定义错误处理程序未捕获错误

时间:2016-03-16 18:50:00

标签: java xml dom xsd

我正在编写一个系统来检查XML和XSD是否正确绑定,但是,我遇到了一个没有捕获异常的问题。

这是我根据XSD检查XML的Dom代码

public static String simpleBindingCheck(File xmlFile, File xsdFile) throws ValidationException
{

    DocumentBuilderFactory  factoryValidation = DocumentBuilderFactory.newInstance();
    factoryValidation.setValidating(false);
    factoryValidation.setNamespaceAware(true);

    SchemaFactory schemaFactory = 
            SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

    try
    {
        factoryValidation.setSchema(schemaFactory.newSchema(
                new Source[] {new StreamSource(xsdFile)}));
    }
    catch (SAXException e1)
    {
        throw new ValidationException("Failed to add schema to Sax binding check..",e1);
    }

    try
    {
        DocumentBuilder builder = factoryValidation.newDocumentBuilder();
        Document document = builder.parse(xmlFile);
    }
    catch (ParserConfigurationException | SAXException e)
    {
        throw new ValidationException(ValidationConstants.SYSTEM_FAILED,e);     
    }
    catch (IOException e)
    {
        throw new ValidationException(ValidationConstants.OBJECT_NOT_FOUND,e);
    }
    catch (Exception e)
    {
        throw new ValidationException("HELLO",e);
    }

    return ValidationConstants.VALID;
}

我的自定义错误处理程序:

public class ValidationException extends Exception
{

    private static final long   serialVersionUID    = 8896968022913351960L;

    public ValidationException()
    {
    }

    public ValidationException(String message)
    {
        super(message);
    }

    public ValidationException(Throwable cause)
    {
        super(cause);
    }

    public ValidationException(String message, Throwable cause)
    {
        super(message, cause);
    }

    public ValidationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)
    {
        super(message, cause, enableSuppression, writableStackTrace);
    }


}

为什么它会在错误输出时返回true ...

错误记录...

Mar 16, 2016 1:42:36 PM com.test.validation.commandline.CommandLineParser fileCheck
INFO: File existed: C:\Users\Dalton\AppData\Local\Temp\generic_-1321812453-14581529972162511000931907374699.xml
Mar 16, 2016 1:42:36 PM com.test.validation.commandline.CommandLineParser fileCheck
INFO: File existed: C:\Users\Dalton\AppData\Local\Temp\generic_-1321812453-14581529972161999314701696764654.xsd
[Error] generic_-1321812453-14581529972162511000931907374699.xml:2:90: cvc-elt.1: Cannot find the declaration of element 'Product'.
Mar 16, 2016 1:42:36 PM com.test.validation.commandline.CommandLineParser commandDecisionLogic
INFO: XML and XSD binding is valid with Dom: Valid

1 个答案:

答案 0 :(得分:0)

我在我的代码中添加了以下代码来修复它:

public class ParserLoggingErrorHandler implements ErrorHandler {


    private boolean isValid = true;
    private String errorMessage = "";

    public boolean isValid() {
        return this.isValid;
    }

    public String errorMessage() {
        return this.errorMessage;
    }

    @Override
    public void warning(SAXParseException exc) {
        // log info
        this.isValid = false;
        this.errorMessage = exc.toString();
    }

    @Override
    public void error(SAXParseException exc) {
         // log info
        this.isValid = false;
        this.errorMessage = exc.toString();
    }

    @Override
    public void fatalError(SAXParseException exc) throws SAXParseException {
        // log info
        this.isValid = false;
        this.errorMessage = exc.toString();
    }


}

并使构建器具有以下特定处理程序:

builder.setErrorHandler(errorHandler);