有没有一种方法可以编写 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();
关于java - dom4j - xpath - 获取 xml 作为 Document.valueOf 的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31161292/