我有一个调用网络服务的客户端,我正在返回一个 ElementNSImpl
对象。
是否可以将此对象转换为字符串?
对于 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);
}
}
最佳答案
您可以从 ElementNSImpl
对象中获取 org.w3c.dom.Document
。
在这里:
ElementNSImpl eleNsImplObject =somehowGetThatElement();
org.w3c.dom.Document document = eleNsImplObject.getOwnerDocument();
希望这对您有所帮助。
关于java - ElementNSImpl 到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16276538/