[英]dom4j - xpath - get xml as string for Document.valueOf
Is there a way to write XPath expression that will be returning xml tree as string when called with Document.valueOf
method from dom4j library? 有没有一种方法可以编写XPath表达式,当使用dom4j库中的
Document.valueOf
方法调用该表达式时,它将以字符串形式返回xml树? I tried /node()
, but it returns just texts in all nodes, not including tags. 我尝试了
/node()
,但是它只返回所有节点中的文本,不包括标签。
Method 1 : 方法1:
String result = document.toXML().toString();
Method 2 : (a more elegant aproach) 方法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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.