Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@IsurangaPerera
Last active July 27, 2017 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IsurangaPerera/373652c3693e1cca688863d030736dbc to your computer and use it in GitHub Desktop.
Save IsurangaPerera/373652c3693e1cca688863d030736dbc to your computer and use it in GitHub Desktop.
public SOAPMessage buildSoapResponse(Source response) throws SOAPException {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage soapMsg = factory.createMessage();
SOAPPart part = soapMsg.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPBody body = envelope.getBody();
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = tf.newTransformer();
transformer.transform(response, result);
} catch (TransformerException e) {
e.printStackTrace();
}
DocumentBuilder db = null;
DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
dFact.setNamespaceAware(true);
try {
db = dFact.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
}
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(writer.toString()));
Document doc = null;
try {
doc = db.parse(is);
} catch (SAXException | IOException e) {
e.printStackTrace();
}
body.addDocument(doc);
return soapMsg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment