如何让XSLT在Java中返回UTF-8 [英] How can I get XSLT to return UTF-8 in Java

查看:125
本文介绍了如何让XSLT在Java中返回UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的XSL脚本使用UTF-8编码。像åäö和希腊字符这样的人物只是像垃圾一样变成了。使其工作的唯一方法是将结果写入文件。如果我把它写到一个输出流,它只返回垃圾(System.out工程,但这可能是因为它的蜜蜂重定向到一个文件)。

I'm trying to get my XSL script to work with UTF-8 encoding. Characters like åäö and greek characters just turn up like garbage. The only way to get it to work is if I write the result to a file. If I write it to an output stream it only returns garbage (System.out works, but that might be because its beeing redirected to a file).

结果需要从servlet返回,请注意,它不是servlet配置问题。我可以从servlet返回一个带有希腊字符的硬编码字符串,它的工作正常,所以这是一个转换的问题。

The result needs to be returned from a servlet, and please note that its not a servlet configuration issue. I can return a hard coded string with greek characters from the servlet and it works fine, so it's an issue with the transformation.

这是我当前的(简化的)代码。

Here is my current (simplified) code.

protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
IOException {
    try {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        final TransformerFactory factory = this.getFactory();

        final File inFile = new File("infile.xml");
        final File xslFile = new File("template.xsl");
        final File outFile = new File("outfile.html");

        final Templates templates = factory.newTemplates(new StreamSource(xslFile));
        final Transformer transformer = templates.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        final InputStream in = new FileInputStream(inFile);
        final StreamSource source = new StreamSource(in);

        final StreamResult result1 = new StreamResult(outFile);
        final StreamResult result2 = new StreamResult(System.out);
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final StreamResult result3 = new StreamResult(out);

        //transformer.transform(source, result1);
        //transformer.transform(source, result2);
        transformer.transform(source, result3);

        final Writer writer = response.getWriter();
        writer.write(new String(out.toByteArray()));
        writer.close();
        in.close();

    } catch (final TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (final TransformerException e) {
        e.printStackTrace();
    }
}

此外,我的XSL脚本包含以下

Also, my XSL script contains the following

<xsl:output method="html" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

什么是正确的方式让这个工作?如果可能有任何帮助,我正在使用Saxon进行转换。

What is the correct way to get this to work? I'm using Saxon for the transformation if that might be of any help.

推荐答案

这几乎肯定是问题: p>

This is almost certainly the problem:

writer.write(new String(out.toByteArray()));

您已仔细地将文本编码为UTF-8,然后转换为字符串使用平台默认编码。您应该几乎不要使用使用平台默认编码的 String 构造函数和方法。即使您希望使用该编码,请明确地进行。

You've carefully encoded your text as UTF-8, and then you're converting into a string using the platform default encoding. You should pretty much never use the String constructors and methods which use the platform default encoding. Even if you want to use that encoding, do so explicitly.

如果您要写入 Writer 反正为什么你开始写一个 ByteArrayOutputStream ?为什么不直接去 Writer

If you're going to write to a Writer anyway, why are you starting off writing to a ByteArrayOutputStream? Why not go straight to the Writer?

然而,直接写入响应的输出会更好流( response.getOutputStream()),并设置响应的内容类型以表示它是UTF-8。

It would be better, however, to write straight to the response's output stream (response.getOutputStream()), and also set the response's content type to indicate that it's UTF-8.

请注意,如果您真的希望事先将结果作为 String 获取,请使用 StringWriter 。没有必要写一个 ByteArrayOutputStream 然后转换成一个字符串。

Note that if you really want to get the result as a String beforehand, use StringWriter. There's no point in writing to a ByteArrayOutputStream and then converting to a string.

这篇关于如何让XSLT在Java中返回UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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