Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在使用StAX验证XML时,尝试从自定义错误处理程序引发自定义异常时出现问题_Java_Xml_Exception_Xsd_Stax - Fatal编程技术网

Java 在使用StAX验证XML时,尝试从自定义错误处理程序引发自定义异常时出现问题

Java 在使用StAX验证XML时,尝试从自定义错误处理程序引发自定义异常时出现问题,java,xml,exception,xsd,stax,Java,Xml,Exception,Xsd,Stax,我试图在针对XSD验证XML时实现一个自定义异常,以便返回错误中的行号和元素 但是,当我在错误处理程序中抛出自定义异常时,会抛出一个SAXException,消息中包含我的异常,而不是我的自定义错误 当我的CustomXMLValidationException被抛出到CustomErrorHandler中时,我希望它被捕获到validateXml(): 以下是CustomErrorHandler: public class CustomErrorHandler implements Error

我试图在针对
XSD
验证
XML
时实现一个自定义异常,以便返回错误中的行号和元素

但是,当我在错误处理程序中抛出自定义异常时,会抛出一个
SAXException
,消息中包含我的异常,而不是我的自定义错误

当我的
CustomXMLValidationException
被抛出到
CustomErrorHandler
中时,我希望它被捕获到
validateXml()

以下是
CustomErrorHandler

public class CustomErrorHandler implements ErrorHandler {

    private boolean isValid = true;

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

    private XMLStreamReader reader;

    public CustomErrorHandler(XMLStreamReader reader) {
        this.reader = reader;
    }

    @Override
    public void error(SAXParseException exc) throws SAXException {
        throw new CustomXmlValidationException(exc.getMessage(), exc.getLineNumber(), reader.getLocalName());
    }

    @Override
    public void fatalError(SAXParseException exc) {

    }

    @Override
    public void warning(SAXParseException e) {

    }
CustomXmlValidationException

public class CustomXmlValidationException extends RuntimeException {

    int lineNo;
    String element;

    public CustomXmlValidationException(String message, int lineNo, String element) {
        super(message);
        this.lineNo = lineNo;
        this.element = element;
    }

    public int getLineNo() {
        return this.lineNo;
    }

    public String getElement() {
        return element;
    }
}
public class CustomXmlValidationException extends RuntimeException {

    int lineNo;
    String element;

    public CustomXmlValidationException(String message, int lineNo, String element) {
        super(message);
        this.lineNo = lineNo;
        this.element = element;
    }

    public int getLineNo() {
        return this.lineNo;
    }

    public String getElement() {
        return element;
    }
}