Java Utililty Methods SOAP Message

List of utility methods to do SOAP Message

Description

The list of methods to do SOAP Message are organized into topic(s).

Method

ListgetNamespaceURIs(SOAPMessage soap)
Returns namespace URIs from a SOAPMessage.
List<String> nsURIs = new ArrayList<>();
SOAPEnvelope envelope = soap.getSOAPPart().getEnvelope();
Iterator<?> it = envelope.getNamespacePrefixes();
while (it.hasNext()) {
    nsURIs.add(envelope.getNamespaceURI((String) it.next()));
return nsURIs;
NodegetNode(Node node, String nodeName)
Goes through all the child nodes of the given node and returns the first child that matches the given name.
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
    if (node.getChildNodes().item(i).getNodeType() == Node.ELEMENT_NODE
            && node.getChildNodes().item(i).getLocalName().equals(nodeName)) {
        return (Node) node.getChildNodes().item(i);
return null;
StringgetPrefix(Name name)
get Prefix
String prefix = name.getPrefix();
return prefix == null || prefix.length() == 0 ? null : prefix;
StringgetPrefix(String namespaceURI, SOAPElement contextElem)
get Prefix
Iterator prefixIt = contextElem.getVisibleNamespacePrefixes();
while (prefixIt.hasNext()) {
    String prefix = (String) prefixIt.next();
    if (namespaceURI.equals(contextElem.getNamespaceURI(prefix)))
        return prefix;
return null;
DocumentgetSOAPData(SOAPMessage soapMessage)
Given a SOAPMessage , get the content as a Document
return soapMessage.getSOAPBody().extractContentAsDocument();
StringgetSoapFaultExceptionMessage(SOAPFaultException sfe)
get Soap Fault Exception Message
if (sfe.getFault().hasDetail()) {
    return sfe.getFault().getDetail().getFirstChild().getLocalName();
if (sfe.getFault().getFaultString() != null) {
    return "\n Message: " + sfe.getFault().getFaultString();
return sfe.getMessage();
SOAPHeadergetSOAPHeader(SOAPMessageContext smc)
Returns the header.
return smc.getMessage().getSOAPPart().getEnvelope().getHeader() == null
        ? smc.getMessage().getSOAPPart().getEnvelope().addHeader()
        : smc.getMessage().getSOAPPart().getEnvelope().getHeader();
SOAPHeaderElementgetSOAPHeaderElement(SOAPMessage message, String headerName, String nameSpace)
get SOAP Header Element
Iterator it = message.getSOAPHeader().examineAllHeaderElements();
while (it.hasNext()) {
    SOAPHeaderElement she = (SOAPHeaderElement) it.next();
    String uri = she.getNamespaceURI();
    String localName = she.getLocalName();
    if (headerName.equals(localName) && nameSpace.equals(uri)) {
        return she;
return null;
DocumentgetSOAPMessage(InputStream in)
A method to get the SOAP message from the input stream.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document soapMessage = db.newDocument();
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new StreamSource(in), new DOMResult(soapMessage));
return soapMessage;
SOAPMessagegetSOAPMessage(InputStream is)
Given a stream of SOAPMessage , construct the SOAPMessage
MessageFactory messageFactory = MessageFactory.newInstance();
return messageFactory.createMessage(null, is);