1
有沒有一種方法來編寫XPath表達式,當用dom4j庫中的Document.valueOf
方法調用時將返回xml樹作爲字符串?我試過/node()
,但它只返回所有節點中的文本,不包括標籤。dom4j-xpath - 獲取xml作爲字符串Document.valueOf
有沒有一種方法來編寫XPath表達式,當用dom4j庫中的Document.valueOf
方法調用時將返回xml樹作爲字符串?我試過/node()
,但它只返回所有節點中的文本,不包括標籤。dom4j-xpath - 獲取xml作爲字符串Document.valueOf
方法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'方法的代碼,我無法改變它 –