我正在寻找同一个问题的答案。您可能已经找到了答案,但对于像我这样到达此线程的其他人。
据我所知,它是特定于实现的。SAX 似乎并没有真正的规范(规范是他们的网站 saxproject.org)。至少我在规范或 javadoc(也许我忽略了)中没有找到任何 isValidating 应该与验证功能同步的参考。
public boolean isValidating()
Indicates if this SAXParserFactory is configured to produce parsers that validate
XML documents as they are parsed.
但是,例如,如果您查看 Xerces 的实现:org.apache.xerces.jaxp.SAXParserFactoryImpl,您会注意到它们在内部使用验证设置 ( isValidation
, setValidating
) 来实现验证功能。
或者,如果您查看 Sun 源
public void setValidating(boolean validating) {
putInFeatures(VALIDATION_FEATURE, validating);
}
(示例来自 openjdk-6-src-b31-15_apr_2014)
另一个是来自Oracle XDK的 oracle xmlparserv2
一些示例测试来证明这一点(您需要 oracle xmlparserv2 jar,并且您还需要一个 META-INF/services/javax.xml.parsers.SAXParserFactory 文件,org.apache.xerces.jaxp.SAXParserFactoryImpl
因为 oracle jar 是……相当占主导地位)
public class SaxParserTest {
@Test
public void validationFeatureTestXerces() throws Exception {
SAXParserFactory parserFactory = org.apache.xerces.jaxp.SAXParserFactoryImpl.newInstance();
parserFactory.setFeature("http://xml.org/sax/features/validation", false);
Assert.assertFalse(parserFactory.isValidating());
parserFactory.setFeature("http://xml.org/sax/features/validation", true);
Assert.assertTrue(parserFactory.isValidating());
parserFactory.setValidating(true);
Assert.assertTrue(parserFactory.getFeature("http://xml.org/sax/features/validation"));
parserFactory.setValidating(false);
Assert.assertFalse(parserFactory.getFeature("http://xml.org/sax/features/validation"));
}
@Test
public void validationFeatureTestOracle() throws Exception {
SAXParserFactory parserFactory = oracle.xml.jaxp.JXSAXParserFactory.newInstance();
parserFactory.setFeature("http://xml.org/sax/features/validation", false);
Assert.assertFalse(parserFactory.isValidating());
parserFactory.setFeature("http://xml.org/sax/features/validation", true);
Assert.assertTrue(parserFactory.isValidating());
parserFactory.setValidating(true);
Assert.assertTrue(parserFactory.getFeature("http://xml.org/sax/features/validation"));
parserFactory.setValidating(false);
Assert.assertFalse(parserFactory.getFeature("http://xml.org/sax/features/validation"));
}
}