如何强制javax xslt转换器使用utf-8而不是html实体编码国家字符? [英] How to force javax xslt transformer to encode national characters using utf-8 and not html entities?
本文介绍了如何强制javax xslt转换器使用utf-8而不是html实体编码国家字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究应该使用某些样式表转换输出的过滤器.代码的重要部分如下所示:
I'm working on filter that should transform an output with some stylesheet. Important sections of code looks like this:
PrintWriter out = response.getWriter();
...
StringReader sr = new StringReader(content);
Source xmlSource = new StreamSource(sr, requestSystemId);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setParameter("encoding", "UTF-8");
//same result when using ByteArrayOutputStream xo = new java.io.ByteArrayOutputStream();
StringWriter xo = new StringWriter();
StreamResult result = new StreamResult(xo);
transformer.transform(xmlSource, result);
out.write(xo.toString());
问题在于,国家字符被编码为html实体,而不是使用UTF.有什么方法可以强制变压器使用UTF-8而不是实体?
The problem is that national characters are encoded as html entities and not by using UTF. Is there any way to force transformer to use UTF-8 instead of entities?
推荐答案
您需要将输出方法设置为text
,而不是(默认)xml
.
You need to set the output method to text
instead of (default) xml
.
transformer.setOutputProperty(OutputKeys.METHOD, "text");
但是,您还应该预先设置响应编码:
You should however also set the response encoding beforehand:
response.setCharacterEncoding("UTF-8");
并指示网络浏览器使用相同的编码:
And instruct the webbrowser to use the same encoding:
response.setContentType("text/html;charset=UTF-8");
这篇关于如何强制javax xslt转换器使用utf-8而不是html实体编码国家字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文