我确信我不是第一个遇到这种冲突的人。
我继承的代码执行以下操作:
org.w3c.dom.Document dom; // declaration
javax.xml.validation.Schema schema; // declaration
...
...
...
javax.xml.validation.Validator validator = schema.newValidator();
validator.validate(new DOMSource(dom));
代表...
看似不重要/不相关的代码
使用 JDK 6 编译和运行代码是可行的(并且总是......)
最近,我不得不将公司其他地方编写的另一个组件集成到我的代码中。该组件绝对需要包含在xercesImpl-2.8.1.jar
我绝对需要这个 3rd 方组件,但现在运行上面的代码不再有效,我得到以下信息:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Root'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Validator.java:127)
作为一种解决方案,我曾想过以某种方式将 xercesImpl-2.8.1.jar 屏蔽在它自己的类加载器中,但没有设法这样做,可能是由于缺乏类加载器知识,或者可能是因为它不是要走的路. 关于我的环境的另一件事,我的应用程序在 tomcat 5.5 和 6 上运行......
顺便说一句,在调试时我注意到当我运行时dom.getImplementation()
- 添加
xercesImpl-2.8.1.jar
到类路径时,结果是org.apache.xerces.dom.DeferredDOMImplementationImpl@5f15c
- 删除它时,结果是
com.sun.org.apache.xerces.internal.dom.DeferredDOMImplementationImpl@6c6ae3
[我想,对于细心的读者来说,这并不奇怪]
有什么建议么?