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编码