Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Strings from SOAP Message in Java

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!

like image 337
o.o Avatar asked May 20 '16 14:05

o.o


People also ask

How do I print a SOAP message?

SOAPMessage has writeTo() method which is used to print its content into a stream.

How do you convert SOAPElement to string?

SOAPElement string = body. addChildElement(envelope. createName( "string" , "abc" , "http://abc.com" ));

How to convert string into SOAP message?

Convert the String into an input stream, then read it into the SOAP message factory. InputStream is = new ByteArrayInputStream(send. getBytes()); SOAPMessage request = MessageFactory. newInstance().


1 Answers

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,

like image 150
albciff Avatar answered Oct 03 '22 01:10

albciff