java ElementNSImpl 到字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16276538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:29:36  来源:igfitidea点击:

ElementNSImpl to String

javaxmlweb-servicesxerces

提问by Sergiu

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

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

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

对于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);
        }
    }

回答by Pramod

You can get org.w3c.dom.Documentfrom ElementNSImplobject. Here it is:

您可以org.w3c.dom.DocumentElementNSImpl对象中获取。这里是:

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

Hope this helps.

希望这可以帮助。