java jdk自带xml编程,使用transform中文乱码问题

String xmlStr = null;
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t;
try {
    t = tf.newTransformer();
    t.setOutputProperty("encoding", "GB2312");// 解决中文问题
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    t.transform(new DOMSource(document), new StreamResult(bos));
    xmlStr = bos.toString();
} catch (TransformerConfigurationException e) {
    LOGGER.error("TransformerConfiguration错误", e);
} catch (TransformerException e) {
    LOGGER.error("Transformer错误", e);
}
 
 

使用 gb2312编码

猜你喜欢

转载自blog.csdn.net/zhengbingmei/article/details/80282229
今日推荐