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;
}
}