java - ElementNSImpl 到字符串

标签 java xml web-services xerces

我有一个调用网络服务的客户端,我正在返回一个 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/

相关文章:

android - 如何替换从 Xml 文件获取的字符串的过程

java - 带有自定义 JAX-B 绑定(bind)的 JAX-WS MarshalException : Unable to marshal type "java.lang.String" as an element

java - 如何初始化Map<Integer, Map<Integer, Float>>?

java - 如何使用 Morphia 获取 MongoDB 的所有非结构化字段

xml - 使用 Linux Bash 脚本从 XML 文件中获取数据

java - 如何在Android中实例化多个自定义 View ?

java - 从工资总额和工资税中查找净工资

java - 将 4 个整数包装在 64 位长中 - java 按位

apache-flex - 灵活的网络服务客户端

java - 如何向 cxf soap 请求添加自定义 header ?