如何使用 Java 使用 XSL 转换 XML [英] How to transform XML with XSL using Java

查看:33
本文介绍了如何使用 Java 使用 XSL 转换 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用标准的 javax.xml.transform 库通过 XSL 将我的 XML 转换为 CSV.我的 XSL 文件很大 - 大约 950 行.我的 XML 文件也可能很大.

I am currently using the standard javax.xml.transform library to transform my XML to CSV using XSL. My XSL file is large - at around 950 lines. My XML files can be quite large also.

它在原型阶段运行良好,只有大约 50 行左右的 XSL 的一小部分.现在,在最终系统"中执行转换时会出现错误分支目标偏移量太大.

It was working fine in the prototype stage with a fraction of the XSL in place at around 50 lines or so. Now in the 'final system' when it performs the transform it comes up with the error Branch target offset too large for short.

private String transformXML() {
    String formattedOutput = "";
    try {

        TransformerFactory tFactory = TransformerFactory.newInstance();            
        Transformer transformer =
                tFactory.newTransformer( new StreamSource( xslFilename ) );

        StreamSource xmlSource = new StreamSource(new ByteArrayInputStream( xmlString.getBytes() ) );
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        transformer.transform( xmlSource, new StreamResult( baos ) );

        formattedOutput = baos.toString();

    } catch( Exception e ) {
        e.printStackTrace();
    }

    return formattedOutput;
}

我遇到了一些关于此错误的帖子,但不太确定该怎么做.
我在代码方面做错了什么吗?是否有任何替代的 3rd Party 转换器可以做到这一点?

I came across a few postings on this error but not too sure what to do.
Am I doing anything wrong code wise? Are there any alternative 3rd Party transformers available that could do this?

谢谢,

安德斯

推荐答案

我在网上看到一篇提到 apache XALAN 的帖子.所以我将罐子添加到我的项目中.尽管我没有在我的代码中直接引用任何 XALAN 类,但一切都开始工作了.据我所知,它仍然应该使用 jaxax.xml 类.

I came across a post on the net that mentioned apache XALAN. So I added the jars to my project. Everything has started working since even though I do not directly reference any XALAN classes in my code. As far as I can tell it still should use the jaxax.xml classes.

不太确定那里发生了什么.但它正在工作.

Not too sure what is happening there. But it is working.

这篇关于如何使用 Java 使用 XSL 转换 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆