我正在寻找相同问题的答案。你可能已经找到了答案,但对于像我这样来到这个线程的其他人来说。
据我所见,它是实现特定的。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"));
}
}