Search code examples
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:

  • Use the javax.xml.parsers.SAXParserFactory system property.
  • UseUse the JAVA_HOME(the parent directory where jdk is installed)/lib/jaxp.properties for a property file that contains the name of the implementation class keyed on the same value as the system property defined above.
  • Use the Services API (as detailed in teh JAR specification), if available, to determine the classname. The Services API will look for a classname in the file META-INF/services/javax.xml.parsers.SAXParserFactory in jars available to the runtime.
  • Platform default SAXParserFactory instance.

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).