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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37349144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:13:56  来源:igfitidea点击:

Get Strings from SOAP Message in Java

javaxmlweb-servicessoapwsdl

提问by o.o

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

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

For example, if the .wsdlmessage is this:

例如,如果.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>

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

我想获取someMsg值并将其保存到字符串变量中。

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!

我在看这个:Get SoapBody Element value,但并没有真正理解。如果有人可以提供解释或任何类型的指南,将不胜感激!

采纳答案by albciff

The normal way to create a client to deal with SOAPmessages and web serviceare; generate the beans from the .xsdschemas, and all the stubs from .wsdlto invoke the web service(in this case for Javafor example could be using JAXWSand JAXB).

创建客户端以处理SOAP消息和Web 服务的正常方法是;从.xsd模式和所有存根生成 bean.wsdl以调用Web 服务(在这种情况下,对于Java,例如可以使用JAXWSJAXB)。

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

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

Anyway of course you can invoke a web serviceusing directly and apache http clientor 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.SOAPMessagesince seems that you want to use this class based on the links you put in the question).

无论如何,当然您可以直接使用apache http 客户端或左右调用Web 服务来进行 POST 然后处理响应......但请注意,这不是处理来自 SOAP 的大量请求和响应的推荐方法Web 服务,因为这样您就必须手动解析每个响应才能开展业务。假设这是您的情况,您可以执行与此类似的操作来处理您的SOAP 消息(我使用,因为您似乎想根据您在问题中放置的链接使用此类)。javax.xml.soap.SOAPMessage

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

例如,如果您收到一条 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>

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 Stringinstead from a File.

它也适用于Java 8,现在我唯一的猜测是FileInputStream. 您可以尝试以下相同的代码,但从File获取请求,String而不是从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,

希望能帮助到你,