When I run a simple function for updating DOM XML with no jars, it runs correctly. If I put it code into an existing project with many jars, I get this exception
Exception in thread "main" java.lang.RuntimeException: net.sf.saxon.trans.XPathException: DOMSource cannot be processed: check that saxon9-dom.jar is on the classpath
here
transformer.transform(source, result);
where
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(docBuilder.parse(filepath));
How can I solve this without introducing a dependency on this jar? Thanks.
This message is from an older version of Saxon, and you could solve it either by putting saxon9-dom on the classpath as suggested, or by replacing the Saxon jar file with a more recent release.
Generally I would advise against using the JAXP TransformerFactory mechanism for loading your XSLT processor. You need to decide which XSLT processor you want to use, test your application with it, and then make sure that you load this processor in production regardless what might be lying around on the classpath. If you want your application to work with multiple XSLT processors then you can achieve that but it should be done in a controlled way.
From API doc for TransformerFactory.html#newInstance(), we can see that there are several ways to achieve this.
-Djavax.xml.transform.TransformerFactory=<any transformer class you need>.META-INF\services\javax.xml.transform.TransformerFactory and this file only contains the default transformer factory of JDK: com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl. And then add it to the beginning of the classpath.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With