dom4j - xpath - 将xml作为Document.valueOf的字符串

时间:2015-07-01 12:26:37

标签: java xpath dom4j

有没有办法编写XPath表达式,当用dom4j库中的Document.valueOf方法调用时,它会将xml树作为字符串返回?我尝试了/node(),但它只返回所有节点中的文本,不包括标签。

1 个答案:

答案 0 :(得分:0)

方法1:

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(); 

方法2:(更优雅的方法)

public abstract class Bar {
    public int count = 0;
}