解决dom4j document.asXML内容为空时自闭合的问题
主要代码:
format.setExpandEmptyElements(true);
若为false则自闭合
//转换为标准格式(避免自闭合的问题)
private String asXml(Document document){
OutputFormat format = new OutputFormat();
format.setEncoding("UTF-8");
format.setExpandEmptyElements(true);
StringWriter out = new StringWriter();
XMLWriter writer = new XMLWriter(out, format);
try {
writer.write(document);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
return out.toString();
}
效果: <postrace/> 变为 <postrace></postrace>
(文/wangfei0904306)
本文来源:http://blog.csdn.net/wangfei0904306/article/details/71634128