本文介绍了ElementNSImpl to String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用Web服务的客户端,我正在返回一个 ElementNSImpl 对象。
是否可以将此对象转换为String?

I have a client that calls a web-service and I'm getting back an ElementNSImpl object.Would it be possible to transform this object to String?

对于 org.w3c.dom.Document 对象,我使用过这样的代码:

For a org.w3c.dom.Document object I've used such code:

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
这是:

You can get org.w3c.dom.Document from ElementNSImpl object. Here it is:

 ElementNSImpl eleNsImplObject =somehowGetThatElement();
 org.w3c.dom.Document document = eleNsImplObject.getOwnerDocument();

希望这会有所帮助。

这篇关于ElementNSImpl to String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 23:41