您的位置:首页 > 其它

怎么样把xml Document对象转换成字符串

2007-12-04 08:28 309 查看
public String doc2String(Document doc) {
String str = null;

try{
TransformerFactory factory = TransformerFactory.newInstance();
Transformer trans = factory.newTransformer();
trans.setOutputProperty(OutputKeys.ENCODING, "gb2312"); // 处理汉字编码

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
StreamResult strOut = new StreamResult(byteOut);

Source xmlSource = new DOMSource(doc);

trans.transform(xmlSource, strOut);

str = strOut.toString();
}catch(Exception e){
e.printStackTrace();
}

return str;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: