ElementNSImpl to String

血红的双手。 提交于 2019-12-22 06:45:06

问题


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?

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

回答1:


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

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

Hope this helps.



来源:https://stackoverflow.com/questions/16276538/elementnsimpl-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!