【问题标题】:dom4j - xpath - get xml as string for Document.valueOfdom4j - xpath - 获取 xml 作为 Document.valueOf 的字符串
【发布时间】:2015-07-01 12:26:36
【问题描述】:

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

【问题讨论】:

    标签: java xpath dom4j


    【解决方案1】:

    方法一:

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

    【讨论】:

    • 我需要 XPath 表达式,而不是 Java 代码。不幸的是,我已经有一个使用 Documend.valueOf 方法的代码,我无法更改它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多