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