dasp
dasp

Reputation: 907

How to force use of specific XML parser

I have Xerces and Oracle XML parsers both in my application's classpath (don't ask why).

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.

Is there a way I can force/tell the classloader to use the Xerces parces when constructing the document builder factory?

Upvotes: 4

Views: 6379

Answers (3)

schlm3
schlm3

Reputation: 108

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

Could fix this using the system property -Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser

Upvotes: 0

skaffman
skaffman

Reputation: 403581

DocumentBuilderFactory has a newInstance() method where you can specify the class name of the implementation you want to use.

Upvotes: 3

KJP
KJP

Reputation: 519

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.

We ultimately fixed it by adding -D parameters to our runtime to force the correct classes:

-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

Upvotes: 5

Related Questions