【发布时间】:2015-07-01 12:26:36
【问题描述】:
有没有办法编写 XPath 表达式,当使用 dom4j 库中的 Document.valueOf 方法调用时,该表达式将返回 xml 树作为字符串?我试过/node(),但它只返回所有节点中的文本,不包括标签。
【问题讨论】:
有没有办法编写 XPath 表达式,当使用 dom4j 库中的 Document.valueOf 方法调用时,该表达式将返回 xml 树作为字符串?我试过/node(),但它只返回所有节点中的文本,不包括标签。
【问题讨论】:
方法一:
String result = document.toXML().toString();
方法2:(更优雅的方法)
DOMSource domSource = new DOMSource(document);
StringWriter writer = new StringWriter();
StreamResult streamResult = new StreamResult(writer);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory .newTransformer();
transformer.transform(domSource, streamResult);
String result = writer.toString();
【讨论】:
Documend.valueOf 方法的代码,我无法更改它