从Java中获取SOAP消息中的字符串

时间:2016-05-20 14:19:29

标签: java xml web-services soap wsdl

如何从SOAP消息中获取特定部分并获取其值?

例如,如果.wsdl消息是这样的:

<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>

我想得到someMsg值并将其保存到String变量中。

我在看这个:Get SoapBody Element value,但并不是很了解。如果有人可以提供解释或任何类型的指南,我们将非常感激!

1 个答案:

答案 0 :(得分:7)

创建客户端以处理 SOAP 消息和 Web服务的常规方法是:从.xsd模式生成bean,并从.wsdl生成所有存根以调用 Web服务(在本例中为 Java ,例如可以使用 JAXWS JAXB )。

另请注意,通常.wsdl定义服务,但如果您询问如何解析请求最好​​显示.xsd

无论如何,您当然可以直接使用 web服务 apache http客户端左右来进行POST,然后处理响应......但请注意这一点不是推荐的方法来处理来自SOAP Web服务的大量请求和响应,因为那时您需要手动解析每个响应以使您的业务。假设这是你的情况,你可以做类似的事情来处理你的 SOAP消息(我使用javax.xml.soap.SOAPMessage,因为似乎你想根据你放入的链接使用这个类。题)。

例如,如果您收到的SOAP消息如下:

<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>

您可以执行以下操作:

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);
    }

}

基于评论的编辑:

它也适用于 Java 8 ,现在我的唯一猜测就是FileInputStream发生了一些事情。您是否可以尝试使用相同的跟随代码但是从String获取请求而不是文件

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);
    }
}

希望它有所帮助,