使用xslt将.xml,转换成一个html网页时中文显示乱码怎么办,用dom将数据写到xml中 设置xml文件gbk编码时中文会出现乱码...

问题:我用dom将数据写到xml中 设置xml文件的编码格式是gbk时再dos下运行jar包中文会出现乱码

如下:

1338345912_2205.png

代码如下:

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包,就没问题了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值