如何强制javax xslt转换器使用utf-8编码国家字符而不是html实体?
我正在研究filter,它应该使用一些样式表来转换输出。 代码的重要部分如下所示:
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而不是实体?
您需要将输出方法设置为text
而不是(default) xml
。
transformer.setOutputProperty(OutputKeys.METHOD, "text");
但是,您还应该事先设置响应编码:
response.setCharacterEncoding("UTF-8");
并指示webbrowser使用相同的编码:
response.setContentType("text/html;charset=UTF-8");
- 游泳池什么时候改变?
- Java SimpleDateFormat:错误一小时
- java.lang.NoClassDefFoundError:org / apache / commons / collections / Transformer
- 我是否需要关闭PreparedStatement
- 你可以从子接口那个接口的接口调用父接口的默认方法吗?
- JScrollPane中的JEditorPane没有根据需要resize
- 为什么增强的for循环比循环的正常更有效
- 无法阻止ant生成编译器Sun专有的API警告
- ClassNotFoundException org.apache.mahout.math.VectorWritable