ElementNSImpl to String

Ser*_*giu 5 java xml web-services xerces

我有一个调用Web服务的客户端,我正在收回一个ElementNSImpl对象.是否可以将此对象转换为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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

小智 9

你可以org.w3c.dom.DocumentElementNSImpl对象获得.这里是:

 ElementNSImpl eleNsImplObject =somehowGetThatElement();
 org.w3c.dom.Document document = eleNsImplObject.getOwnerDocument();
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.