如何强制 javax xslt 转换器使用 utf-8 而不是 html 实体对国家字符进行编码?
我正在研究应该使用某些样式表转换输出的过滤器。代码的重要部分如下所示:
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 而不是实体?
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());
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将输出方法设置为
text
而不是(默认)xml
。但是,您还应该预先设置响应编码:
并指示网络浏览器使用相同的编码:
You need to set the output method to
text
instead of (default)xml
.You should however also set the response encoding beforehand:
And instruct the webbrowser to use the same encoding: