4
我有一个客户端调用web服务,我回来了一个ElementNSImpl
对象。 是否有可能将此对象转换为字符串?ElementNSImpl to String
对于org.w3c.dom.Document
对象我用这样的代码:
protected static String documentToString(final Document doc) {
// outputs a DOM structure to plain String
try {
StringWriter sw = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(doc), new StreamResult(sw));
return sw.toString();
} catch (Exception ex) {
throw new RuntimeException("Error converting to String", ex);
}
}