如何设置Javax.xml.transform.TransformerFactory系统属性

How to set Javax.xml.transform.TransformerFactory system property

我正在使用 javax.xml.transform.Transform 将 xml 文件转换为 PDF。这本身就可以正常工作,但项目的某些部分正在使用 Xalan,它实现了自己的 TransformerFactory,其中的某些内容不适用于 Cyrillic。

我在 https://xml.apache.org/xalan-j/usagepatterns.html 发现有一个 属性 用来定义使用的工厂 :

TransformerFactory is an abstract class with a static newInstance() method that instantiates the concrete subclass designated by the javax.xml.transform.TransformerFactory system property.

The default setting for this system property is org.apache.xalan.processor.TransformerFactoryImpl.

我的问题是:如何设置此 属性 以不使用 Xalan?

尝试将其设置为 java 参数 -Djavax.xml.transform.TransformerFactory=<factory class>

您也可以直接在程序中实例化所需的工厂 TransformerFactory tf = new SomeTransformerFactoryImpl();

要么在执行程序时从命令行静态设置它,使用 -D 标志:

java -Dorg.apache.xalan.processor.TransformerFactoryImpl=com.xyz.YourFactory YourApp

或者从您的应用程序中动态使用 System.setProperty():

System.setProperty("org.apache.xalan.processor.TransformerFactoryImpl",
        "com.xyz.YourFactory");

请注意,您需要使用工厂 class 的完全限定 class 名称,并且工厂需要在您的 class 路径上才能使其中任何一个工作.