我用dom将数据写到xml中 设置xml文件的编码格式是gbk时中文会出现乱码
代码如下:Transformer t = tf.newTransformer();
t.setOutputProperty(OutputKeys.ENCODING, "gbk");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml");
//设置缩进
t.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource s = new DOMSource(doc);
FileOutputStream output = new FileOutputStream(new File("D:\\user2.xml"));
OutputStreamWriter outwriter = new OutputStreamWriter(output, "gbk");
StreamResult res = new StreamResult(outwriter );
t.transform(s, res);
outwriter.close();
output.close();但是设置成utf-8时 就不会有问题
代码如下:Transformer t = tf.newTransformer();
t.setOutputProperty(OutputKeys.ENCODING, "gbk");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml");
//设置缩进
t.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource s = new DOMSource(doc);
FileOutputStream output = new FileOutputStream(new File("D:\\user2.xml"));
OutputStreamWriter outwriter = new OutputStreamWriter(output, "gbk");
StreamResult res = new StreamResult(outwriter );
t.transform(s, res);
outwriter.close();
output.close();但是设置成utf-8时 就不会有问题
LZ为什么会有问题呢。另外,字符集名称写成GB2312这样的形式更规范一些。
要不然 我就用utf-8了