How to enforce a specific XML parser

I have Xerces and Oracle XML syntax like in my application class path (don't ask why).

When I create a new javax.xml.parsers.DocumentBuilderFactory , the class loader automatically picks up the Oracle XML parser. However, this is not a complete / correct implementation, so it gives me headaches.

Is there a way to get / tell the class loader to use the Xerces options when building the factory document constructor?

+4
source share
2 answers

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

+3
source

In my large project, the scaffman's answer would work all the time, but not on time, because we have several subprojects that depend on these libraries. We looked at the source at javax.xml.transform.TransformerFactory.newInstance () and found that it uses javax.xml.transform.FactoryFinder.find ("javax.xml.transform.TransformerFactory", ...). This method then checks the system parameter to determine the correct implementation.

We eventually fixed this by adding -d options 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.perser. .jaxp.DocumentBuilderFactoryImpl

+3
source

Source: https://habr.com/ru/post/1316633/


All Articles