2013-04-29 106 views
4

我有一個客戶端調用web服務,我回來了一個ElementNSImpl對象。 是否有可能將此對象轉換爲字符串?ElementNSImpl to 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); 
     } 
    } 

回答

8

您可以從ElementNSImpl對象獲取org.w3c.dom.Document。 這就是:

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

希望這有助於。