javajakarta-eexml-parsingsaxsaxparser

Specific multiple SAXParserFactory implementations in one web application


I have a web application on tomcat. The application needs two SAXParserFactory implements: one is JDK default implement com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl, the other one is from xerces: org.apache.xerces.jaxp.SAXParserFactoryImpl.

Here's the problem, SAXParserFactory.newInstance() just selects only one implementation by specific order, as follows:

I tried to configure the different classnames in javax.xml.parsers.SAXParserFactory in different jar files. But it's scope for whole classloader. In tomcat, all application libs are loaded in one classloader. For instance, the configure file in A.jar will overwrite the configure in B.jar, eventually all application gets the configure from A.jar.

So my question is how can I specific these two SAXParserFactory implementations in one web application? Thank you.


Solution

  • Pretty sure you can't (unless you use multiple classloaders). The code which depends on the other implementation should instantiate it directly (using the implementation class name).