解决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>

本文介绍了解决DOM4J中自闭合元素在输出XML时变为展开形式的方法。通过设置OutputFormat的expandEmptyElements属性为true,可以确保空元素以标准格式输出,例如将<postrace/>转换为<postrace></postrace>。
315

被折叠的 条评论
为什么被折叠?



