怎么样把xml Document对象转换成字符串
发布网友
发布时间:2022-04-23 21:35
我来回答
共1个回答
热心网友
时间:2023-10-10 11:53
/***
* Document象转换String
* @param document
* @return
*/
public String transformXMLToString(Document document) {
try {
XMLOutputter xmlout = new XMLOutputter();
Format tFormat = Format.getPrettyFormat();
tFormat.setEncoding("GBK");
xmlout.setFormat(tFormat);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
xmlout.output(document, bo);
String xmlStr = bo.toString();
bo.flush();
bo.close();
return xmlStr;
} catch (Exception e) {
LogUtil.getLogger().error(LogUtil.getExceptionDetail(e));
return null;
}
}