Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElementNSImpl to 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?

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);
        }
    }
like image 915
Sergiu Avatar asked Apr 29 '13 10:04

Sergiu


1 Answers

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.

like image 62
Pramod Avatar answered Oct 23 '22 04:10

Pramod