[英]Java SAXParser False Positives
I am trying to build my first XML schema validator as a reusable component throughout my codebase and many projects. 我试图在我的代码库和许多项目中构建我的第一个XML模式验证器作为可重用的组件。 I have spent all day trying to follow examples and coding them up, and now have a proof of concept up and running.
我花了一整天的时间来尝试跟踪示例并对其进行编码,现在有了概念验证和运行。
The only problem is, its giving me false positives : its validating XML instances that should absolutely be failing. 唯一的问题是,它给了我误报 :它的验证XML实例绝对应该失败。 I've tested it out on 3 schemas: 1 schema it worked beautifully with, and now its misbehaving with the last two (false positives).
我已经在3个模式上测试了它:1个模式它与之相配得很漂亮,现在它与最后两个模式行为不当(误报)。 I believe its because the first schema/instance pair I tried were extremely simple.
我相信它,因为我尝试的第一个模式/实例对非常简单。 I'm now trying to use it on more complex examples and it is choking.
我现在正试图在更复杂的例子上使用它而且它很窒息。
Here is the body of the validate
method where the SAX validation is done: 以下是完成SAX验证的
validate
方法的主体:
schema = getSchemaAsString();
targetXml = "ijeioj489fu4u8";
SAXParserFactory oSAXParserFactory = SAXParserFactory.newInstance();
SAXParser oSAXParser = null;
oSAXParserFactory.setNamespaceAware(true);
try
{
SchemaFactory oSchemaFactory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
oSAXParserFactory.setSchema(oSchemaFactory.newSchema(new SAXSource(new InputSource(new StringReader(schema)))));
oSAXParser = oSAXParserFactory.newSAXParser();
DefaultHandler handler = new DefaultHandler();
oSAXParser.parse(new InputSource(new StringReader(targetXml)), handler);
}
catch(Exception oException)
{
throw oException;
}
Where schema
and targetXml
are in-memory XML strings ( not file URIs) that are given the following values: schema
和targetXml
是内存中的XML字符串( 不是文件URI),它们具有以下值:
schema
String: schema
字符串:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="PayloadMessage">
<xs:complexType>
<xs:sequence>
<xs:element name="MessageID" type="xs:long"/>
<xs:element name="Timestamp" type="xs:long"/>
<xs:element name="MessageAction" type="xs:string"/>
<xs:element name="ContentType" type="xs:string"/>
<xs:element name="ContentID" type="xs:string"/>
<xs:element name="Payload" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Obviously, the given targetXml
should fail against its given schema. 显然,给定的
targetXml
应该对其给定的模式失败。 Nope. 不。 No exceptions get thrown anywhere inside the SAX stuff.
没有异常会在SAX内部的任何地方被抛出。
I have a feeling I need to do something with the DefaultHandler
but not sure... I went to http://www.w3.org/2001/03/webdata/xsv and confirmed that my schema is valid. 我有一种感觉我需要对
DefaultHandler
做一些事情但不确定......我去了http://www.w3.org/2001/03/webdata/xsv并确认我的架构是有效的。
Does anything jump out at anyone? 任何人都会跳出什么事吗? Thanks in advance!
提前致谢!
You must set an error handler that will throw SAXException
. 您必须设置一个将抛出
SAXException
的错误处理程序。 The default behavior is to attempt parse document even if it isn't valid. 默认行为是尝试解析文档,即使它无效。
DefaultHandler
implements ErrorHandler
but the implementation in case of error or warning does nothing . DefaultHandler
实现了ErrorHandler
但是在出现错误或警告的情况下执行什么都不做 。
Javadoc WARNING: If an application does not register an ErrorHandler, XML parsing errors will go unreported, except that SAXParseExceptions will be thrown for fatal errors.
Javadoc警告:如果应用程序未注册ErrorHandler,则XML解析错误将不会报告,除了因为致命错误而抛出SAXParseExceptions。 In order to detect validity errors, an ErrorHandler that does something with error() calls must be registered.
为了检测有效性错误,必须注册使用error()调用执行某些操作的ErrorHandler。
I recommed this excellent tutorial with examples on XML validation. 我推荐这个优秀的教程和XML验证的例子。 It was most helpful for me.
这对我最有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.