使用xslt将.xml,转换成一个html网页时中文显示乱码怎么办,用dom将数据写到xml中 设置xml文件gbk编码时中文会出现乱码...
問題:我用dom將數據寫到xml中 設置xml文件的編碼格式是gbk時再dos下運行jar包中文會出現亂碼
如下:
代碼如下:
StringWriter strWtr = new StringWriter();
StreamResult strResult = new StreamResult(strWtr);
TransformerFactory tfac = TransformerFactory.newInstance();
try {
Transformer t = tfac.newTransformer();
// t.setOutputProperty(OutputKeys.ENCODING, "GBK");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html,// text
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", spaceNum);
Element element = doc.getDocumentElement();
t.transform(new DOMSource(element), strResult);
} catch (Exception e) {
e.printStackTrace();
// ToolKit.writeLog(this.getClass().getName(), "轉換XML異常,參數為:" + doc, e);
}
result = strResult.getWriter().toString();
至于是因為原因,我也不知道,關于操作XML ,有dom4j這么好用的包,你不用,干啥要用DOM這么原生。
這個我修改把程序環境編碼換成了GBK編碼,甚至把myeclipse的編碼也換成了GBK編碼,問題還是沒有解決。
最后把dom轉換成jdom就解決了,
轉換代碼如下:
/*
把dom轉換成jdom
*/
DOMBuilder builder = new DOMBuilder();
org.jdom.Document jdomDoc = builder.build(doc); //doc:已經創建好的dom
Format format=Format.getPrettyFormat();
format.setEncoding("GBK");
XMLOutputter output=new XMLOutputter();
output.setFormat(format);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
output.output(jdomDoc, baos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result=baos.toString(); //轉換成字符串輸出再運行jar包,就沒問題了
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的使用xslt将.xml,转换成一个html网页时中文显示乱码怎么办,用dom将数据写到xml中 设置xml文件gbk编码时中文会出现乱码...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: signature=806a32b3c9
- 下一篇: Html制作知识库管理系统,HTML 编