有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

编译XSL转换的java使用

我正在制作汇编。使用由org实现的TransformerFactory从XSL转换文件中创建类文件(Translet)。阿帕奇。沙兰。xsltc。特拉克斯。TransformerFactoryImpl

不幸的是,尽管搜索了几个小时,我还是找不到如何在XML转换中使用这些translet类的方法

您是否可以提供任何代码示例或参考文档?因为this文档不足且复杂。 谢谢


共 (1) 个答案

  1. # 1 楼答案

    XSLT中的标准转换如下所示:

        public void translate(InputStream xmlStream, InputStream styleStream, OutputStream resultStream) {
            Source source = new StreamSource(xmlStream);
            Source style = new StreamSource(styleStream);
            Result result = new StreamResult(resultStream);
    
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer t = tFactory.newTransformer(style);
            t.transform(source, result);
        }
    

    因此,考虑到您不使用Transformer工厂,而是使用现成的Java类(这是一个额外的维护难题,由于在初始编译后可以保留Transformer对象,因此不会给您带来更好的性能),相同的函数将如下所示:

        public void translate(InputStream xmlStream, OutputStream resultStream) {
            Source source = new StreamSource(xmlStream);
            Result result = new StreamResult(resultStream);
    
            Translet t = new YourTransletClass();
            t.transform(source, result);
        }
    

    在搜索过程中,您错过了type the Interface specification into Google,其中3rd link显示了接口定义,它与Transformer具有相同的调用签名。因此,您可以将transformer对象替换为自定义对象(或将transformer对象保留在内存中以供重用)

    希望有帮助