Get Strings from SOAP Message in Java

31,922

The normal way to create a client to deal with SOAP messages and web service are; generate the beans from the .xsd schemas, and all the stubs from .wsdl to invoke the web service (in this case for Java for example could be using JAXWS and JAXB).

Note also that typically .wsdl define the service, but If you ask how to parse a request is better to show the .xsd.

Anyway of course you can invoke a web service using directly and apache http client or so to make a POST and then process the response... but note that this is not a recommended way to deal with a lot of request and response from a SOAP web service because then you've to parse manually each response to make your business. Supposing that this is your case you can do something similar to this to process your SOAP message ( I use javax.xml.soap.SOAPMessage since seems that you want to use this class based on the links you put in the question).

For example if you're receiving a SOAP message like:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
    <theRequest>
        <username>user</username>
        <password>password</password>
        <someMsg>sooomeMessage</someMsg>
      </theRequest>
   </soapenv:Body>
</soapenv:Envelope>

You can do something like:

import java.io.FileInputStream;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPMessage;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SOAPMessageTest {

    public static void main(String[] args) throws Exception {

        // create message factory
        MessageFactory mf = MessageFactory.newInstance();
        // headers for a SOAP message
        MimeHeaders header = new MimeHeaders();     
        header.addHeader("Content-Type", "text/xml");

        // inputStream with your SOAP content... for the 
        // test I use a fileInputStream pointing to a file
        // which contains the request showed below
        FileInputStream fis = new FileInputStream("/path/yourSOAPReq.xml");

        // create the SOAPMessage
        SOAPMessage soapMessage = mf.createMessage(header,fis);
        // get the body
        SOAPBody soapBody = soapMessage.getSOAPBody();
        // find your node based on tag name
        NodeList nodes = soapBody.getElementsByTagName("someMsg");

        // check if the node exists and get the value
        String someMsgContent = null;
        Node node = nodes.item(0);
        someMsgContent = node != null ? node.getTextContent() : "";

        System.out.println(someMsgContent);
    }

}

EDIT BASED ON COMMENTS:

It works for me also in Java 8, right now my only guess is that something is happening with the FileInputStream. Can you try the follow code which is the same but get the request from a String instead from a File.

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPMessage;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SOAPMessageTest {

    public static void main(String[] args) throws Exception {

        // create message factory
        MessageFactory mf = MessageFactory.newInstance();
        // headers for a SOAP message
        MimeHeaders header = new MimeHeaders();     
        header.addHeader("Content-Type", "text/xml");

        String request = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
         "<soapenv:Body>"+
           "<theRequest>"+
             "<username>user</username>"+
             "<password>password</password>"+
             "<someMsg>sooomeMessage</someMsg>"+
           "</theRequest>"+
          "</soapenv:Body>"+
        "</soapenv:Envelope>";
        InputStream is = new ByteArrayInputStream(request.getBytes());

        // create the SOAPMessage
        SOAPMessage soapMessage = mf.createMessage(header,is);
        // get the body
        SOAPBody soapBody = soapMessage.getSOAPBody();
        // find your node based on tag name
        NodeList nodes = soapBody.getElementsByTagName("someMsg");
        System.out.println(nodes.getClass().getName());
        // check if the node exists and get the value
        String someMsgContent = null;
        Node node = nodes.item(0);
        someMsgContent = node != null ? node.getTextContent() : "";

        System.out.println(someMsgContent);
    }
}

Hope it helps,

Share:
31,922
o.o
Author by

o.o

grawr ';..;'

Updated on May 21, 2020

Comments

  • o.o
    o.o almost 4 years

    How can I get specific parts from a SOAP message and get their values?

    For example, if the .wsdl message is this:

    <wsdl:message name="theRequest">
          <wsdl:part name="username" type="xsd:string"/>
          <wsdl:part name="password" type="xsd:string"/>
          <wsdl:part name="someMsg"  type="xsd:string"/>
    </wsdl:message>
    

    I want to get someMsg value and save it into a String variable.

    I was looking at this: Get SoapBody Element value, but didn't really understand much. If someone could provide an explanation or any kind of guide it would be really appreciated!