I have Xerces and Oracle XML parsers both in my application's classpath (don't ask why).
我的应用程序的类路径中都有Xerces和Oracle XML解析器(不要问为什么)。
When I create a new javax.xml.parsers.DocumentBuilderFactory
, the classloader automatically picks up the Oracle XML parser. However, it's not a full/proper implementation, so it's giving me headaches.
当我创建一个新的javax.xml.parsers.DocumentBuilderFactory时,类加载器会自动选择Oracle XML解析器。但是,它不是一个完整/正确的实现,所以它给我带来了麻烦。
Is there a way I can force/tell the classloader to use the Xerces parces when constructing the document builder factory?
有没有办法在构造文档构建器工厂时强制/告诉类加载器使用Xerces parces?
3 个解决方案
#1
3
DocumentBuilderFactory
has a newInstance()
method where you can specify the class name of the implementation you want to use.
DocumentBuilderFactory有一个newInstance()方法,您可以在其中指定要使用的实现的类名。
#2
4
For my large project, skaffman's answer would have worked MOST of the time, but not ALL of the time because we have multiple sub-projects that depend on these libraries. We looked at the source to javax.xml.transform.TransformerFactory.newInstance(), and found that it uses javax.xml.transform.FactoryFinder.find("javax.xml.transform.TransformerFactory", ...). This method then looks at a system parameter to determine the correct implementation.
对于我的大型项目,skaffman的答案在大部分时间都有效,但不是所有时间,因为我们有多个依赖于这些库的子项目。我们查看了javax.xml.transform.TransformerFactory.newInstance()的源代码,发现它使用了javax.xml.transform.FactoryFinder.find(“javax.xml.transform.TransformerFactory”,...)。然后,此方法查看系统参数以确定正确的实现。
We ultimately fixed it by adding -D parameters to our runtime to force the correct classes:
我们最终通过向运行时添加-D参数来强制使用正确的类来修复它:
-Djavax.xml.transform.TransformerFactory=org.apache.xalan.processor.TransformerFactoryImpl -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl -Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
-Djavax.xml.transform.TransformerFactory = org.apache.xalan.processor.TransformerFactoryImpl -Djavax.xml.parsers.SAXParserFactory = org.apache.xerces.jaxp.SAXParserFactoryImpl -Djavax.xml.parsers.DocumentBuilderFactory = org.apache.xerces .jaxp.DocumentBuilderFactoryImpl
#3
0
I had a similar situation, but our code uses org.xml.sax.helpers.XMLReaderFactory.createXMLReader()
to create the sax-parser (which is still buggy in 8u162).
我有类似的情况,但我们的代码使用org.xml.sax.helpers.XMLReaderFactory.createXMLReader()来创建sax-parser(在8u162中仍然有问题)。
Could fix this using the system property -Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser
可以使用系统属性-Dorg.xml.sax.driver = org.apache.xerces.parsers.SAXParser来修复此问题
#1
3
DocumentBuilderFactory
has a newInstance()
method where you can specify the class name of the implementation you want to use.
DocumentBuilderFactory有一个newInstance()方法,您可以在其中指定要使用的实现的类名。
#2
4
For my large project, skaffman's answer would have worked MOST of the time, but not ALL of the time because we have multiple sub-projects that depend on these libraries. We looked at the source to javax.xml.transform.TransformerFactory.newInstance(), and found that it uses javax.xml.transform.FactoryFinder.find("javax.xml.transform.TransformerFactory", ...). This method then looks at a system parameter to determine the correct implementation.
对于我的大型项目,skaffman的答案在大部分时间都有效,但不是所有时间,因为我们有多个依赖于这些库的子项目。我们查看了javax.xml.transform.TransformerFactory.newInstance()的源代码,发现它使用了javax.xml.transform.FactoryFinder.find(“javax.xml.transform.TransformerFactory”,...)。然后,此方法查看系统参数以确定正确的实现。
We ultimately fixed it by adding -D parameters to our runtime to force the correct classes:
我们最终通过向运行时添加-D参数来强制使用正确的类来修复它:
-Djavax.xml.transform.TransformerFactory=org.apache.xalan.processor.TransformerFactoryImpl -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl -Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
-Djavax.xml.transform.TransformerFactory = org.apache.xalan.processor.TransformerFactoryImpl -Djavax.xml.parsers.SAXParserFactory = org.apache.xerces.jaxp.SAXParserFactoryImpl -Djavax.xml.parsers.DocumentBuilderFactory = org.apache.xerces .jaxp.DocumentBuilderFactoryImpl
#3
0
I had a similar situation, but our code uses org.xml.sax.helpers.XMLReaderFactory.createXMLReader()
to create the sax-parser (which is still buggy in 8u162).
我有类似的情况,但我们的代码使用org.xml.sax.helpers.XMLReaderFactory.createXMLReader()来创建sax-parser(在8u162中仍然有问题)。
Could fix this using the system property -Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser
可以使用系统属性-Dorg.xml.sax.driver = org.apache.xerces.parsers.SAXParser来修复此问题