如何在解组时使用格式不正确的 XML 进行验证?
我有一个解组器和一个 MySchema.xsd 文件。
StreamSource sources = new StreamSource(getClass().getClassLoader().getResourceAsStream("/xmlValidation.xsd"));
SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
unmarshaller.setSchema(sf.newSchema(sources));
并调用 unmarshaller.setEventHandler() 函数,指定自定义验证事件处理程序,该处理程序基本上格式化错误提示字符串,方法是:
final String errorString = new String();
unmarshaller.setEventHandler(new ValidationEventHandler() {
@Override
public boolean handleEvent(ValidationEvent validationevent) {
if(validationevent.getSeverity()!= ValidationEvent.WARNING){
errorString.format( "Line:Col[" + validationevent.getLocator().getLineNumber()
+ ":" + validationevent.getLocator().getColumnNumber()
+ "]:" + validationevent.getMessage());
return false;
}
return true;
}
});
上面的代码似乎工作正常(当输入字符串经过验证时,我可以获取 java 对象。并且错误提示字符串的格式也为例外)
问题是,当输入的 xml 格式不正确时,它也会抛出 SaxParseException。
提前致谢。 安德鲁
I have an unmarshaller along with an MySchema.xsd file.
StreamSource sources = new StreamSource(getClass().getClassLoader().getResourceAsStream("/xmlValidation.xsd"));
SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
unmarshaller.setSchema(sf.newSchema(sources));
And make a call to unmarshaller.setEventHandler() function, to specify a custom validation event handler, which basically format a error tips string , by:
final String errorString = new String();
unmarshaller.setEventHandler(new ValidationEventHandler() {
@Override
public boolean handleEvent(ValidationEvent validationevent) {
if(validationevent.getSeverity()!= ValidationEvent.WARNING){
errorString.format( "Line:Col[" + validationevent.getLocator().getLineNumber()
+ ":" + validationevent.getLocator().getColumnNumber()
+ "]:" + validationevent.getMessage());
return false;
}
return true;
}
});
The above codes seem work ok(I can get java object when the input string is validated. and also the error tips string is formated as excepted)
The problem is that, when the input xml is not well form, it also throw a SaxParseException.
Thanks in advance.
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
格式良好与 XML 语法本身相关,而不是有效的 WRT XML 模式:
如果您的 XML 格式不正确,那么您将收到 ValidationEvent.FATAL_ERROR 并且解组将无法继续,因为 JAXB 使用的底层解析器无法继续。
了解更多信息:
Well formed relates to the XML syntax itself, as opposed to being valid WRT an XML schema:
If you have XML that is not well formed then you will get a ValidationEvent.FATAL_ERROR and unmarshalling will not be able to continue, as the underlying parser used by JAXB cannot continue.
For more information:
K,我搞砸了一些事情并遇到了这个问题。
现在我明白了。如果我错了,请指出我。下面是我在 javadoc 中找到的内容并在我的项目上进行测试:
当解组器解组时,javax.xml.bind.ValidationEventHandler 可以处理给定模式约束的约束错误。
如果发生错误,将在解组过程中调用 ValidationEventHandler。
如果 xmlInputStream 格式不正确,则会抛出 SAXEception。
而且我无法找到一种方法来捕获由 sax 解析器抛出的 SAXException,因此我猜想在解组期间使用验证不能由于格式不正确的 xml 字符串而导致。
我使用 javax.xml.validation.Validator 来验证 xml 字符串格式正确且受约束。
上面的代码会抛出SAXException。
如果没有抛出异常,则将 xml 字符串解组到对象中。
K, I mess up something and get this problem.
Now I figure it out. If I am wrong, please point me out. below it's what I find in javadoc and test on my project:
javax.xml.bind.ValidationEventHandler can handler the constrain error with the given schema constrains, when unmarshaller is unmarshaling.
The ValidationEventHandler will be called during the unmarshaling process if error occurs.
The SAXEception will be thrown, if the xmlInputStream is not well form.
And I cant find a way to catch the SAXException, throw by the sax parser, so I guess using validation during unmarshaling can't due with un-well form xml string.
I use javax.xml.validation.Validator to validate that the xml string is well form and under constrain.
The above code will throw SAXException.
If no exception is thrown, then unmarshall the xml string into object.