How to convert SOAPBody to String

后端 未结 3 508
梦如初夏
梦如初夏 2020-12-11 01:02

I want to convert SOAPBody to String. What is the best way to do it? Should i first convert it to xml and then convert it into String or we can jsut convert it into String.<

3条回答
  •  难免孤独
    2020-12-11 01:42

    Figured this might help -

    private String convertToString (SOAPBody message) throws Exception{
       Document doc = message.extractContentAsDocument();
       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();
      }
    

    Thanks to the following post - XML Document to String?

提交回复
热议问题